Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the best way to measure execution time of a function? [duplicate]

Obviously I can do and DateTime.Now.After - DateTime.Now.Before but there must be something more sophisticated.

Any tips appreciated.

like image 861
Ian G Avatar asked Dec 19 '08 13:12

Ian G


People also ask

How does Visual Studio measure execution time?

If milliseconds then in Visual Studio 2019 you can see the time between two breakpoints under Diagnostic Tools -> Events -> Duration (opens automatically in Debug mode, or use Ctrl + Alt + F2 ).


2 Answers

System.Environment.TickCount and the System.Diagnostics.Stopwatch class are two that work well for finer resolution and straightforward usage.

See Also:

  • Is DateTime.Now the best way to measure a function’s performance?
  • High resolution timer in .NET
  • Environment.TickCount vs DateTime.Now
  • What’s the best way to benchmark programs in Windows?
like image 137
ctacke Avatar answered Sep 21 '22 23:09

ctacke


I would definitely advise you to have a look at System.Diagnostics.Stopwatch

And when I looked around for more about Stopwatch I found this site;

Beware of the stopwatch

There mentioned another possibility

Process.TotalProcessorTime

like image 42
tafa Avatar answered Sep 23 '22 23:09

tafa