Section 6.8. Time Your Code


6.8. Time Your Code

Timing code isn't difficult. You can use the DateTime.Now property to capture the current date and time down to the millisecond. However, this approach isn't perfect. Constructing the DateTime object takes a short time, and that little bit of latency can skew the time you record for short operations. Serious profilers need a better approach, one that uses low-level systems calls and has no latency.


Note: The new Stopwatch class allows you to track how fast your code executes with unparalleled precision.

6.8.1. How do I do that?

In .NET 2.0, the best way to time your code is to use the new Stopwatch class in the System.Diagnostics namespace. The Stopwatch class is refreshingly simple to use. All you need to do is create an instance and call the Start( ) method. When you're finished, call Stop( ).

Example 6-8 shows a simple test that times how long a loop takes to finish. The elapsed time is then displayed in several different ways, with different degrees of precision.

Example 6-8. Timing a loop
Module TimeCode          Sub Main( )         Dim Watch As New Stopwatch( )              Watch.Start( )              ' Delay for a while.         For i As Integer = 1 To 1000000000         Next              Watch.Stop( )              ' Report the elasped time.         Console.WriteLine("Milliseconds " & Watch.ElapsedMilliseconds)         Console.WriteLine("Ticks: " & Watch.ElapsedTicks)         Console.WriteLine("Frequency: " & Stopwatch.Frequency)         Console.WriteLine("Whole Seconds: " & Watch.Elapsed.Seconds)         Console.WriteLine("Seconds (from TimeSpan): " & Watch.Elapsed.TotalSeconds)         Console.WriteLine("Seconds (most precise): " & _           Watch.ElapsedTicks / Stopwatch.Frequency)     End Sub      End Module

Here's the output you'll see:

Milliseconds 10078 Ticks: 36075265 Frequency: 3579545 Whole Seconds: 10 Seconds (from TimeSpan): 10.0781705 Seconds (most precise): 10.078170549609

You can retrieve the elapsed time in milliseconds from the Stopwatch.ElapsedMilliseconds property. (One second is 1,000 milliseconds.) The ElapsedMilliseconds property returns a 64-bit integer (a Long), making it extremely precise. If it's more useful to retrieve the time as a number of seconds or minutes, use the Stopwatch.Elapsed property instead, which returns a TimeSpan object.

On the other hand, if you want the greatest possible precision, retrieve the number of ticks that have elapsed from the Stopwatch.ElapsedTicks property. Stopwatch ticks have a special meaning. When you use the TimeSpan or DateTime object, a tick represents 0.0001 of a millisecond. In the case of a Stopwatch, however, ticks represent the smallest measurable increment of time, and depend on the speed of the CPU. To convert Stopwatch ticks to seconds, divide ElapsedTicks by Frequency.

6.8.2. What about...

...pausing a timer? If you want to record the total time taken to complete multiple operations, you can use Stop( ) to pause a timer and Start( ) to resume it later. You can then read the total time taken for all the operations you timed from the Elasped and ElaspedMilliseconds properties.

You can also run multiple timers at once. All you need to do is create one Stopwatch object for each distinct timer you want to use.



Visual Basic 2005(c) A Developer's Notebook
Visual Basic 2005: A Developers Notebook
ISBN: 0596007264
EAN: 2147483647
Year: 2006
Pages: 123

Similar book on Amazon

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net