Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# replacement for PerformanceCounter

I'm wondering if there is a ".net integrated" solution for exact time measurement (like the execution time of a function)? At the moment I'm using the PerformanceCounters from Kernel32.

    [DllImport("Kernel32.dll")]
    private static extern bool QueryPerformanceCounter(
        out long lpPerformanceCount);

    [DllImport("Kernel32.dll")]
    private static extern bool QueryPerformanceFrequency(
        out long lpFrequency);
like image 384
Haiko Wick Avatar asked Jul 11 '26 04:07

Haiko Wick


2 Answers

The Stopwatch is a high resolution framework timer class (that probably wraps that api).

like image 65
BC. Avatar answered Jul 13 '26 19:07

BC.


Try

  • System.Environment.TickCount
  • System.Diagnostics.Stopwatch

Both of these work well for finer resolution and straightforward usage.

You could also see

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

or

What's the best way to benchmark programs in Windows?

like image 38
Ian G Avatar answered Jul 13 '26 18:07

Ian G



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!