Connecting Tech Pros Worldwide Help | Site Map

Timer Function for Determining Code Execution Speed

 
LinkBack Thread Tools Search this Thread
  #1  
Old November 13th, 2005, 06:07 AM
laurenq uantrell
Guest
 
Posts: n/a
Default Timer Function for Determining Code Execution Speed

I have been using the following function to test the speed of various
functions, however, quite often the return value is zero.
I'm hoping someone can help improve on this.

Function TimeIt() As Single
On Error GoTo CodeErr
'PURPOSE: Times a process in seconds from start to finish
'USAGE: msgbox TimeIt

Dim sngStart As Single
Dim sngEnd As Single
Dim sngElapsed As Single
Dim sngAverage As Single
Dim LoopCount As Long
Dim LoopMax As Long

'set the number of times you want to execute the code.
'the function will return an average speed of these executions:
LoopMax = 100

Do Until LoopCount = LoopMax

'get start time
sngStart = Timer

'run the code or function you wish to test here:

'******TEST CODE START*******


'******TEST CODE START*******

'get end time
sngEnd = Timer

'get elapsed time
sngElapsed = sngEnd - sngStart

LoopCount = LoopCount + 1

'determine the average time of the cumulative loops
sngAverage = (sngAverage + sngElapsed) / LoopCount

Loop

TimeIt = Format(sngAverage, "######0.0000000000")

CodeExit:
Exit Function
CodeErr:
MsgBox Err.Number & " " & Err.Description
Resume CodeExit
End Function


  #2  
Old November 13th, 2005, 06:07 AM
pietlinden@hotmail.com
Guest
 
Posts: n/a
Default Re: Timer Function for Determining Code Execution Speed

How about this....
(taken from vbnet.mvps.org... sorry, for some weird reason I can't get
the real page)

'''''''''''''''''''''''''''''''''''''''''''''''''' ''''''''''''''
' Copyright ©1996-2005 VBnet, Randy Birch, All Rights Reserved.
' Some pages may also contain other copyrights by the author.
'''''''''''''''''''''''''''''''''''''''''''''''''' ''''''''''''''
' Distribution: You can freely use this code in your own
' applications, but you may not reproduce
' or publish this code on any web site,
' online service, or distribute as source
' on any media without express permission.
'''''''''''''''''''''''''''''''''''''''''''''''''' ''''''''''''''

Private Declare Function GetTickCount Lib "kernel32" () As Long

Private tstart As Single

Private Enum ElapsedTimeType
ttMilliseconds = 1
ttSeconds = 2
ttMinutes = 3
End Enum


Private Function GetElapsedTime(dwTimeType As ElapsedTimeType) As
Single

Dim divisor As Long

Select Case dwTimeType
Case ttMilliseconds: divisor = 1
Case ttSeconds: divisor = 1000
Case ttMinutes: divisor = 60000
End Select

GetElapsedTime = (GetTickCount() - tstart) / divisor

End Function


Private Sub Form_Load()

'Assign the current tick count
'as the tick start point. Failure
'to do this will return the number
'of ticks since Windows was last
'rebooted. (Note: elapsed time is
'stored as a DWORD value in Windows.
'Therefore, the time will wrap around
'to zero if the system is run continuously
'for 49.7 days. If a higher resolution
'timer is required, use a multimedia
'or high-resolution timer.
tstart = GetTickCount()

End Sub

Calling Syntax

Print FormatNumber(GetElapsedTime(ttMilliseconds), 0) &
"milliseconds"
Print FormatNumber(GetElapsedTime(ttSeconds), 2) & "
seconds"
Print FormatNumber(GetElapsedTime(ttHours), 2) & "
minutes"

If you search VBNet for "GetTickCount" you'll find all this fun stuff...

  #3  
Old November 13th, 2005, 06:08 AM
laurenq uantrell
Guest
 
Posts: n/a
Default Re: Timer Function for Determining Code Execution Speed

Thanks for pointing me in the right direction. With your help I have
constructed a very useful function for testing the speed of code
execution which I'm posting below to help anyone else who might be
interested in doing the same...

Place this code in a new module:

Option Compare Database
Option Explicit

Public Declare Function GetTickCount Lib "kernel32" () As Long
Public tstart As Single

Private Enum ElapsedTimeType
ttMilliseconds = 1
ttSeconds = 2
ttMinutes = 3
End Enum

Private Function GetElapsedTime(dwTimeType As ElapsedTimeType) As
Single

Dim divisor As Long

Select Case dwTimeType
Case ttMilliseconds: divisor = 1
Case ttSeconds: divisor = 1000
Case ttMinutes: divisor = 60000
End Select

GetElapsedTime = (GetTickCount() - tstart) / divisor

End Function

Function TimeItUsingGetTickCount() As String
On Error GoTo myErr
'PURPOSE: Times a process in milliseconds from start to finish

Dim sngElapsed As Single
Dim sngAverage As Single
Dim sngTotal As Single
Dim LoopCount As Long
Dim LoopMax As Long

'set the number of times you want to run the code
'to find the total time and average time per loop:
LoopMax = 100

Do Until LoopCount = LoopMax

'reset the timer:
tstart = GetTickCount()

'run the code or function you wish to test here:

'******TEST CODE START*******


'******TEST CODE END*******

sngElapsed = GetElapsedTime(ttMilliseconds)

LoopCount = LoopCount + 1

sngTotal = sngTotal + sngElapsed

sngAverage = (sngAverage + sngElapsed) / LoopCount

Loop

TimeItUsingGetTickCount = "Total time for " & LoopMax & "
loops:" & vbCrLf & vbCrLf & FormatNumber(sngTotal, 0) & " milliseconds"
& vbCrLf & vbCrLf & _
"Average time = " & FormatNumber(sngAverage, 0) & "
milliseconds"

myExit:
Exit Function
myErr:
MsgBox Err.Number & " " & Err.Description
Resume myExit
End Function




pietlinden@hotmail.com wrote:[color=blue]
> How about this....
> (taken from vbnet.mvps.org... sorry, for some weird reason I can't[/color]
get[color=blue]
> the real page)
>
> '''''''''''''''''''''''''''''''''''''''''''''''''' ''''''''''''''
> ' Copyright ©1996-2005 VBnet, Randy Birch, All Rights Reserved.
> ' Some pages may also contain other copyrights by the author.
> '''''''''''''''''''''''''''''''''''''''''''''''''' ''''''''''''''
> ' Distribution: You can freely use this code in your own
> ' applications, but you may not reproduce
> ' or publish this code on any web site,
> ' online service, or distribute as source
> ' on any media without express permission.
> '''''''''''''''''''''''''''''''''''''''''''''''''' ''''''''''''''
>
> Private Declare Function GetTickCount Lib "kernel32" () As Long
>
> Private tstart As Single
>
> Private Enum ElapsedTimeType
> ttMilliseconds = 1
> ttSeconds = 2
> ttMinutes = 3
> End Enum
>
>
> Private Function GetElapsedTime(dwTimeType As ElapsedTimeType) As
> Single
>
> Dim divisor As Long
>
> Select Case dwTimeType
> Case ttMilliseconds: divisor = 1
> Case ttSeconds: divisor = 1000
> Case ttMinutes: divisor = 60000
> End Select
>
> GetElapsedTime = (GetTickCount() - tstart) / divisor
>
> End Function
>
>
> Private Sub Form_Load()
>
> 'Assign the current tick count
> 'as the tick start point. Failure
> 'to do this will return the number
> 'of ticks since Windows was last
> 'rebooted. (Note: elapsed time is
> 'stored as a DWORD value in Windows.
> 'Therefore, the time will wrap around
> 'to zero if the system is run continuously
> 'for 49.7 days. If a higher resolution
> 'timer is required, use a multimedia
> 'or high-resolution timer.
> tstart = GetTickCount()
>
> End Sub
>
> Calling Syntax
>
> Print FormatNumber(GetElapsedTime(ttMilliseconds), 0) &
> "milliseconds"
> Print FormatNumber(GetElapsedTime(ttSeconds), 2) & "
> seconds"
> Print FormatNumber(GetElapsedTime(ttHours), 2) & "
> minutes"
>
> If you search VBNet for "GetTickCount" you'll find all this fun[/color]
stuff...

 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,662 network members.