[RESOLVED] Timing VB Code-VBForums
Results 1 to 5 of 5

Thread: [RESOLVED] Timing VB Code

  1. #1

    Thread Starter
    New Member
    Join Date
    Apr 2012
    Posts
    14

    Resolved [RESOLVED] Timing VB Code

    I was wondering if someone knows if there is some type of VB Timer that will time how long your code runs for. I ask this because i am looking for a way to optimize the speed of my code.

  2. #2
    Addicted Member sweet_dreams's Avatar
    Join Date
    Apr 2005
    Location
    Poland, Lodz
    Posts
    189

    Re: Timing VB Code

    Hi,

    To measure time in seconds (and parts of seconds) you can use Timer function. This function returns seconds from Midnight.

    Code:
    Sub TestTime()
       Dim sStartTime As Single
       Dim sEndTime As Single
    
       'Stores start time
       sStartTime = Timer
    
       'Place your code here
    
       'Stores end time
       sEndTime = Timer
    
       'Show execution time
       Debug.Print "Execution time in seconds: ", sEndTime - sStartTime
    End Sub
    Hope this helps.

    Regards,
    using VB 2010 .NET Framework 4.0; MS Office 2010; SQL Server 2008 R2 Express Edition | Remember to mark resolved threads and rate useful posts.

  3. #3
    New Member
    Join Date
    Apr 2012
    Location
    Right in the middle of Europe
    Posts
    12

    Re: Timing VB Code

    Hi,

    I have used the method Sweet Dreams suggests very effectively in the past to do exactly what you want to do.

    Regards,

  4. #4
    Hyperactive Member Granty's Avatar
    Join Date
    Mar 2001
    Location
    London
    Posts
    439

    Re: Timing VB Code

    There is also the TimeGetTime() API function which is very accurate

  5. #5
    Member
    Join Date
    Dec 2011
    Location
    Brisbane, Australia
    Posts
    43

    Re: Timing VB Code

    The code I have used in the past to do this is:

    Code:
    Private Declare Function GetTickCount Lib "kernel32.dll"() As Long
    
    Private Sub Time_Code()
        Dim t as 'Long
        t = GetTickCount
        
        'Put code to be timed here
        
        MsgBox "Code took " & GetTickCount - t & _
            " milliseconds to run.", vbInformation, "Time Taken"
    End Sub

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.