Timer...How to measure an event?

  • Thread starter Thread starter Gary
  • Start date Start date
G

Gary

Hi,


How does one set up a timer so I can measure the time to do an event?

Something like this I guess the format would be,



start timer...

(do event)

....stop timer

display total time in seconds in a msg box




thanks,

Gary
 
Depends on how much precision you need but since you asked for Seconds, try
this:

dim dtStart as DateTime = Now()
....stuff happens here
dim clockIt as Integer = CInt(DateDiff(DateInterval.Second, dtStart, Now())

messagebox.show(clockIt.ToString)


If you need higher precision look to QueryPerformanceCounter, and if you
need timing down to nearly the millisecond then you might want to look to a
multimedia timer. Examples of these are just a google away.

Robert Smith
Kirkland, WA
www.smithvoice.com
 
Gary,

Environment.tickcount does the trick.

Be aware that that is in milliseconds what is not even precious enough
because there is normally so much multiprocessing that you needs to do test
thousands of time to real get an answer.

There is as well the which looks more precise
Declare Function QueryPerformanceCounter Lib "Kernel32" (ByRef X As
Long) As Short

However read what I wrote above.

I hope this helps?

Cor
 
Gary,
In addition to the other comments.

I normally use QueryPerformanceCounter, as its compatible with Performance
Counters. For example I can define a Performance Counter that shows Time Per
Operation very easily with QueryPerformanceCounter & the PerformanceCounter
class...

Following are some KB articles that show you how to use
QueryPerformanceCounter to time your code.

Use QueryPerformanceCounter to Time code in VB.NET:
http://support.microsoft.com/default.aspx?scid=kb;en-us;306978

Use QueryPerformanceCounter to Time code in C#:
http://support.microsoft.com/default.aspx?scid=kb;en-us;306979

Use QueryPerformanceCounter to Time code in C++ .NET:
http://support.microsoft.com/default.aspx?scid=kb;en-us;815668

NOTE: It appears that VS.NET 2005 (aka Whidbey, due out later in 2005) will
have a StopWatch class that internally will decide between
QueryPerformanceCounter & Environment.TickCount:

http://msdn2.microsoft.com/library/ebf7z0sw.aspx

Hope this helps
Jay
 
Back
Top