1

I wanted to test how long it takes a method to run and I was wondering if timespan would be the best way to go with that? I do not want to log the start and end of the method because I heard that logging will not give an accurate time reading because of the time it takes to create and update existing log files. If timespan is not the best way any suggestions on what would be a good way to do this?

Icebreaker
  • 277
  • 1
  • 6
  • 13
  • Might want to check this question out, too: http://stackoverflow.com/questions/2923283/stopwatch-vs-using-system-datetime-now-for-timing-events – JDB Oct 11 '12 at 17:54

5 Answers5

6

It's best to use the Stopwatch class, which was designed for this sort of thing.

http://msdn.microsoft.com/en-us/library/system.diagnostics.stopwatch.aspx

Stopwatch stopWatch = new Stopwatch();
stopWatch.Start();
DoSomething();
stopWatch.Stop();
// Get the elapsed time as a TimeSpan value.
TimeSpan ts = stopWatch.Elapsed;
System Down
  • 6,192
  • 1
  • 30
  • 34
  • 1
    Interesting article comparing various timing methods: http://reflexangle.blogspot.com/2009/04/datetimenow-vs-systemdiagnosticsstopwat.html. `StopWatch` comes out on top. – JDB Oct 11 '12 at 17:52
  • Ahhhh this looks a lot better. Thank you. – Icebreaker Oct 11 '12 at 17:53
  • @Marino3d - IF this solution has worked for you, dont forget to mark it as am accepted answer. – System Down Oct 13 '12 at 01:51
3
var duration = Measure(() => YourFunction(param1));

long Measure(Action action)
{
    Stopwatch sw = Stopwatch.StartNew();
    action();
    return sw.ElapsedMilliseconds;
}
L.B
  • 114,136
  • 19
  • 178
  • 224
2

I use Stopwatch to time my code.

 var sw = new Stopwatch();
 sw.Start()
 //code to time here
 sw.Stop();
 Console.WriteLine("Time taken in milliseconds: " + sw.ElapsedMilliseconds);

You can get pretty fancy with your timings with stop watch. I have used it to test nested executions, as well as average out the time taken for multiple loop iterations.

To test multiple iterations do:

var sw = new Stopwatch();
List<long> totalTime = new List<long>();

for (var u = 0; u < 100000; u++)
{
    sw.Start();
    //logic here
    sw.Stop();
    totalTime.Add(sw.ElapsedMilliseconds);
    sw.Reset();
}
Console.WriteLine("Average time in Milliseconds: {0}", totalTime.Average());
Robert H
  • 11,520
  • 18
  • 68
  • 110
2

You want to use the Stopwatch class.

djs
  • 220
  • 1
  • 5
2

You should use the Stopwatch class:

public void Foo()
{
    var sw = Stopwatch.StartNew();
    // Do work...
    var totalTime = sw.Elapsed;
}

I often make myself a utility method to handle this:

public static void ComputeTimings(Action method, string methodName)
{
    var sw = Stopwatch.StartNew();
    method();
    sw.Stop();
    Debug.WriteLine("Method {0} took {1}", methodName, sw.Elapsed);
}

This can then be called as:

ComputeTimings(() => Foo(bar), "Foo");

This is nice as you can "inject" the timings into your code and remove it as needed, without changing your actual method bodies.

Also note that, for very accurate timings, you often need to execute the method at least twice. The first time a method runs, the timings may be off due to the JIT compiler. You can build that into the above method, if you choose, ie:

public static void ComputeTimings(Action method, string methodName, bool executeOnceFirst = true)
{
    if (executeOnceFirst) 
        method();

    var sw = Stopwatch.StartNew();
    method();
    sw.Stop();
    Debug.WriteLine("Method {0} took {1}", methodName, sw.Elapsed);
}
Reed Copsey
  • 554,122
  • 78
  • 1,158
  • 1,373