|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Announcements
Chapters
Services
Feature Zones
|
Introduction
Creating an application requires precisely measuring how long specific
operations take to complete. You may be searching for performance bottlenecks
prior to shipping, measuring to ensure that changes do not unexpectedly degrade
performance, or (in my case), testing alternative architectural approaches. You
can use a real stopwatch and measure your application's response time, but
fumbling and other miscues will render about a 1/4 second (0.25) resolution, at
best. The
The Windows Kernel includes a much finer-grained tick counter, which
it exposes in an API call named
BackgroundStopwatch merely implements the code described in Microsoft Knowledge Base article 306978 (10/02) in a easily used class. I can't claim any originality - the technical details are by Microsoft, and the "stopwatch" class idea is from an old C++ article I read in 1993 or so (and implemented back then using mmsystem.dll in Windows 3.1; yikes!). Using the codeThere are two ways to use
Here's a very simple way to display how long a process takes on the console: Dim swatch as New Stopwatch("My process")
'Perform my process (presumably a loop or one or more method calls)
swatch.ReportToConsole()
'Output: "My process (0.332131039000767 seconds)"
You can use the same
Dim firstResult, secondResult as Double
Dim swatch as New Stopwatch()
'...perform process...
firstResult = swatch.ElapsedTime() ' Note: ElapsedTime does
' not stop the clock
'...more process work...
swatch.Done()
secondResult = swatch.ElapsedTime()
Console.WriteLine(
"First took {0} seconds; First and Second took {1} seconds.", _
firstResult, secondResult)
swatch.Start()
'...a third bunch of work...
swatch.Done()
MsgBox("The third thing took " & CStr(swatch.ElapsedTime()) & " seconds.")
'Output: "First took 0.0131530683368976 seconds;
'First and Second took 8.8372527793337 seconds."
Points of InterestThis code also manages to demonstrate how to call the Windows API and how to
define a custom exception class. Microsoft Knowledge Base article 172338 describes how to use
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||