How long a particular piece of code took to execute?

The Stopwatch class under the System.Diagnostics namespace is the key. The Stopwatch class makes it easy to use the high resolution timer in your code.


        // Create new stopwatch
 Stopwatch stopwatch = new Stopwatch();
 // Begin timing
 stopwatch.Start();
 // Do something
        // Code Here
 // Stop timing
 stopwatch.Stop();
 // Write result
 Console.WriteLine("Time elapsed: {0}",stopwatch.Elapsed);

No comments:

Post a Comment