Coder Perfect

Calculate a method’s execution time.


I’ve created an I/O timer that replicates data from one location to another. What is the most accurate and reliable method of calculating the execution time? Thread? Timer? Stopwatch? Is there any other option? I’d like the most precise one feasible, as well as the shortest one available.

Asked by Mahdi Tahsildari

Solution #1

Stopwatch is one of the best ways to measure time execution in.NET and is created for this purpose.

var watch = System.Diagnostics.Stopwatch.StartNew();
// the code that you want to measure comes here
var elapsedMs = watch.ElapsedMilliseconds;

In.NET, don’t use DateTime to estimate execution time.


As @series0ne pointed out in the comments area, if you want a really exact assessment of the execution of some code, you’ll have to use the operating system’s performance counters. The following response provides a good overview.

Answered by Darin Dimitrov

Solution #2

The System.Diagnostics, based on personal experience. The Stopwatch class can be used to calculate a method’s execution time, but BEWARE: It is not completely accurate!

Consider the following illustration:

Stopwatch sw;

for(int index = 0; index < 10; index++)
    sw = Stopwatch.StartNew();


Example results


“Well, why did it take 132ms the first time and substantially less the rest of the time?” you might ponder.

Stopwatch does not adjust for “background noise” activity in.NET, such as JITing, according to the answer. As a result, when you run your method for the first time,.NET JITs it. The time it takes to do so is added to the execution time. Other factors, on the other hand, will affect the execution time.

Performance Profiling is what you should be looking for if you want ultimate precision!

Take a look at these examples:

Although the RedGate ANTS Performance Profiler is a commercial tool, it delivers extremely accurate data. – .NET profiling can help you improve the performance of your applications.

A StackOverflow article on profiling can be found here: – Which.NET Profilers Are Worth Using?

You might also be interested in my post on Performance Profiling using Stopwatch – Performance Profiling in.NET.

Answered by Matthew Layton

Solution #3

StopWatch is a class that finds the optimal option for you.

Stopwatch sw = Stopwatch.StartNew();

Console.WriteLine("Time taken: {0}ms", sw.Elapsed.TotalMilliseconds);

It also includes a Stopwatch static field. IsHighResolution. This is, of course, a hardware and operating system problem.

Answered by Soner Gönül

Solution #4

If you want to learn more about performance, the ideal solution is to use a profiler.

Otherwise, System.Diagnostics is the way to go. StopWatch is a timer with a high resolution.

Answered by Jeff Foster

Solution #5

The high-resolution counter will be used by StopWatch.

If you’re measuring IO, external events will almost certainly affect your results, and I wouldn’t care about precision (as you’ve indicated). Instead, I’d collect a number of measures and look at the average and distribution of those numbers.

Answered by Brian Agnew

Post is based on