473,606 Members | 2,200 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(sngAvera ge, "######0.000000 0000")

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

Nov 13 '05 #1
2 7020
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(Ge tElapsedTime(tt Milliseconds), 0) &
"millisecon ds"
Print FormatNumber(Ge tElapsedTime(tt Seconds), 2) & "
seconds"
Print FormatNumber(Ge tElapsedTime(tt Hours), 2) & "
minutes"

If you search VBNet for "GetTickCou nt" 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 TimeItUsingGetT ickCount() 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

TimeItUsingGetT ickCount = "Total time for " & LoopMax & "
loops:" & vbCrLf & vbCrLf & FormatNumber(sn gTotal, 0) & " milliseconds"
& vbCrLf & vbCrLf & _
"Average time = " & FormatNumber(sn gAverage, 0) & "
milliseconds"

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


pi********@hotm ail.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(Ge tElapsedTime(tt Milliseconds), 0) &
"millisecon ds"
Print FormatNumber(Ge tElapsedTime(tt Seconds), 2) & "
seconds"
Print FormatNumber(Ge tElapsedTime(tt Hours), 2) & "
minutes"

If you search VBNet for "GetTickCou nt" 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
3769
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 starttime = Timer() Here is my system setup:
3
1399
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
2556
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 there a better way than using this: While (MyCondition is true) System.Windows.Forms.Application.DoEvents() End While
5
1764
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 System.IO.DirectoryInfo dirFolder = New System.IO.DirectoryInfo(sFilePath) Dim filFiles() As System.IO.FileInfo
3
9605
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 2003, no performance problems. However, when I open the same database in Access 2007, it's as slow as molasses. Data trickles onto the form instead of an immediate display in Access 2003. This happens regardless of whether I keep the Access...
1
2520
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
2007
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, upon executing the code again (without refreshing the browser) the two tests were almost identical in speed. I am not sure if this has something to do specifically with JavaScript (and/or its engine) or if this is just something browsers try...
1
3430
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 = Timer but in VB.NET, while i try to use this timer function, error occurs. i'm not able to use this Timer function in VB.NET. but i need to take time as we take time using Timer(no: of seconds since midnight). anyone have anyidea, how to use...
4
2143
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 app. This is the implementation that works: void findPacketHeaders(char *inputFile, char *outputFile) { /* Open file */ fpInput = fopen(inputFile, "rb"); fpOutput = fopen(outputFile, "w"); /* check if fpInput exists */
0
8015
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8439
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8430
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8094
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8305
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5465
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
3930
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
1553
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1296
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.