Using Stopwatch to benchmark your heavy performance code

Stopwatch is a accurate and super simple utility class included in .NET that can be used for benchmarking your heavy performance code, like sorting a big list or some large calculations.

1
2
3
4
5
6
7
System.Diagnostics.Stopwatch stopwatch = new System.Diagnostics.Stopwatch();
stopwatch.Start();
 
// Run heavy duty code...
 
stopwatch.Stop();
Debug.Log(stopwatch.ElapsedMilliseconds + " ms elapsed in running heavy duty code.");
System.Diagnostics.Stopwatch stopwatch = new System.Diagnostics.Stopwatch();
stopwatch.Start();

// Run heavy duty code...

stopwatch.Stop();
Debug.Log(stopwatch.ElapsedMilliseconds + " ms elapsed in running heavy duty code.");