Coder Perfect

Is DateTime.Now the best approach to evaluate the performance of a function? [closed]


I need to discover a bottleneck and measure time as precisely as feasible.

Is the code snippet below the best technique to measure performance?

DateTime startTime = DateTime.Now;

// Some execution process

DateTime endTime = DateTime.Now;
TimeSpan totalTimeTaken = endTime.Subtract(startTime);

Asked by David Basarab

Solution #1

No, it isn’t. Make use of the stopwatch (in System.Diagnostics)

Stopwatch sw = Stopwatch.StartNew();

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

Stopwatch automatically checks for the existence of high-precision timers.

It’s worth noting that DateTime.Now is frequently significantly slower than DateTime. Because of the work that needs to be done with timezones, DST, and other things, UtcNow was created.

DateTime. The resolution of UtcNow is typically 15 milliseconds. See John Chapman’s DateTime blog article. Now, let’s get down to business with a superb overview.

The stopwatch reverts to DateTime, which is an interesting fact. If your hardware doesn’t support a high frequency counter, use UtcNow instead. By looking at the static field Stopwatch, you may see if Stopwatch employs hardware to attain high precision. IsHighResolution.

Answered by Markus Olsson

Solution #2

If you’re looking for something quick and dirty, I’d recommend Stopwatch for a higher level of precision.

Stopwatch sw = new Stopwatch();
// Do Work

Console.WriteLine("Elapsed time: {0}", sw.Elapsed.TotalMilliseconds);

If you need something a little more advanced, you should probably look into using a third-party profiler like ANTS.

Answered by mmcdole

Solution #3

According to this article, you should first compare three options: Stopwatch, DateTime, and Calendar. Now AND DateTime are both valid. UtcNow.

It also reveals that Stopwatch uses DateTime.UtcNow Plus some extra processing in some circumstances (where a performance counter isn’t available). As a result, it’s clear that DateTime.UtcNow is the best option in this scenario (because others utilize it + some processing).

However, as it turns out, the counter is almost always there – see Explanation of high-resolution performance counter and its existence in relation to.NET Stopwatch?

This is a graph of performance. Consider how inexpensive UtcNow’s performance cost is in comparison to alternatives:

The sample data size is on the X axis, and the example’s relative time is on the Y axis.

Stopwatch has the advantage of providing higher-resolution time measurements. Another factor is its OO character. Creating an OO wrapper for UtcNow, on the other hand, can’t be difficult.

Answered by Valentin Kuzub

Solution #4

It’s a good idea to put your benchmarking code into a utility function or class. On an error, the StopWatch class does not need to be Disposed or Stopped. So, the most basic code for timing an activity is

public partial class With
    public static long Benchmark(Action action)
        var stopwatch = Stopwatch.StartNew();
        return stopwatch.ElapsedMilliseconds;

Sample calling code

public void Execute(Action action)
    var time = With.Benchmark(action);
    log.DebugFormat(“Did action in {0} ms.”, time);

Here’s the latest version of the extension technique.

public static class Extensions
    public static long Benchmark(this Action action)
        return With.Benchmark(action);

Also included is a sample calling code.

public void Execute(Action action)
    var time = action.Benchmark()
    log.DebugFormat(“Did action in {0} ms.”, time);

Answered by Anthony Mastrean

Solution #5

The stopwatch feature may be improved (higher precision). I’d also consider simply installing one of the popular profilers (the ones I’ve used the most are DotTrace and ANTS… DotTrace’s free trial is completely functional and doesn’t nag like some of the others).

Answered by jsight

Post is based on