patterncsharpCritical
Exact time measurement for performance testing
Viewed 0 times
testingtimemeasurementforexactperformance
Problem
What is the most exact way of seeing how long something, for example a method call, took in code?
The easiest and quickest I would guess is this:
But how exact is this? Are there better ways?
The easiest and quickest I would guess is this:
DateTime start = DateTime.Now;
{
// Do some work
}
TimeSpan timeItTook = DateTime.Now - start;But how exact is this? Are there better ways?
Solution
A better way is to use the Stopwatch class:
using System.Diagnostics;
// ...
Stopwatch sw = new Stopwatch();
sw.Start();
// ...
sw.Stop();
Console.WriteLine("Elapsed={0}",sw.Elapsed);Code Snippets
using System.Diagnostics;
// ...
Stopwatch sw = new Stopwatch();
sw.Start();
// ...
sw.Stop();
Console.WriteLine("Elapsed={0}",sw.Elapsed);Context
Stack Overflow Q#969290, score: 784
Revisions (0)
No revisions yet.