473,386 Members | 1,886 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

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

Nov 13 '05 #1
2 6994
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...

Nov 13 '05 #2
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


pi********@hotmail.com wrote:
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...

Nov 13 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: JohnWayne | last post by:
Hello All, I am having a problem calling the Timer function. I get a compile error when I include a call to this function. Invalid character /sirenServer/INCLUDE/test.asp, line 32, column 19...
3
by: linvin333 | last post by:
Hi, Does function overloading affect the speed of execution of the program ? If so what is the reason ? How does it compare with a speed of a program using non-overloded functions ? Linny
3
by: Marty | last post by:
Hi, Is there a "light" way on CPU usage to delay a process in the code execution. For example, if I want a function to wait 5 seconds before continue executing without using a timer. Is...
5
by: Adrian Enders | last post by:
I have something that I have never seen before in a MS development product. I have a pretty simple call to a network directory that looks something like this ... Dim dirFolder As...
3
by: Michael | last post by:
I work with a highly programmed Access database (some 15,000 lines of VBA code, much of it automating data entry on forms -- and believe me, it's very tight code). In Access 97, 2000, 2002, and...
1
by: Kelie | last post by:
hello, would there be any speed increase in code execution after python code being compiled into exe file with py2exe? thanks, kelie
2
by: -Lost | last post by:
I have been watching code execution in various situations and I have noticed something. On the first test my example gave me obvious results. One method was far faster than the other. However,...
1
by: remya1000 | last post by:
I'm using VB.NET. in VB6 we have a function called Timer, which returns a Single value that represents the number of seconds that have elapsed since midnight. for eg: lngStartTimer =...
4
by: bmerlover | last post by:
How can you read a file in a timer function? I am currently using a do while statement to read a file which works fine. The implementation is in C but I've added the code to a C++ .NET framework GUI...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.