Performance Test Stopwatch

For some private projects, I use Stopwatch to measure performance.

But with the low number of call ElapsedMilliseconds that I want to measure, I end up with 0 ElapsedMilliseconds , which makes it difficult to calculate the average.

I was thinking of writing my own stopwatch class. It could calculate with ticks and give undefined ElapsedMicroseconds based on Stopwatch.ElapsedTicks and TimeSpan.TicksPerMillisecond . This will probably not be a very good way.

I definitely need something that is backed up by high-performance winapi counters, so datetime and that won't be enough.

Are there any other ideas?

+4
source share
4 answers

If you received 0 ElapsedMicroseconds, this means that the interval is shorter than 1 ms. You can try measuring periods in Ticks and use the frequency:

  Stopwatch watch = Stopwatch.StartNew(); ... // Estimated code here ... watch.Stop(); // Microseconds int microSeconds = (int)(watch.ElapsedTicks * 1.0e6 / Stopwatch.Frequency + 0.4999); // Nanoseconds (estimation) int nanoSeconds = (int)(watch.ElapsedTicks * 1.0e9 / Stopwatch.Frequency + 0.4999); 
+6
source

StopWatch is what you need. Using:

double diffMs = (stopWatch.ElapsedTicks * 1000.0) / Stopwatch.Frequency;

StopWathch.ElapsedMilliseconds is defined as long. Therefore, it is impossible for it to be more accurate than one millisecond.

+1
source

C # time in microseconds

 long microseconds = ticks / (TimeSpan.TicksPerMillisecond / 1000); 
+1
source

Forgive my ignorance (but), if it is less than a millisecond (1000 ticks), you probably will not have to diagnose it for performance problems

+1
source

Source: https://habr.com/ru/post/1492282/


All Articles