5

For some private project I use Stopwatch for performance measurement.

But on low repitition count of calls I want to measure, I end up with 0 ElapsedMilliseconds, which makes it difficult to calculate an average.

I thought about writing my own Stopwatch class. It could calculate with ticks and give a vague ElapsedMicroseconds based on Stopwatch.ElapsedTicks and TimeSpan.TicksPerMillisecond. This will probably be not a very good way.

I definitly need something that is backed up by the high performance counters of winapi, so datetime and such will not suffice.

Are there any other ideas?

Mare Infinitus
  • 8,024
  • 8
  • 64
  • 113
  • You *will* have to make lots and lots of calls to that function to get a good measurement. You said you wanted an everage. Run a bunch and divide. That's going to give you a much more precise estimate on it's speed. – Randy James Jul 19 '13 at 13:03
  • I'm making 2,000,000 calls and calculate an average. But I think there should be a better way to measure for smaller amounts. – Mare Infinitus Jul 19 '13 at 13:28
  • My point isn't to add together each, but just take the total time for the 2,000,000 and divide. The smaller the measurement you try to take, the more inaccurate it's going to be. – Randy James Jul 19 '13 at 17:54

4 Answers4

7

If you got 0 ElapsedMicroseconds, that means that the interval is shorter than 1 ms. You may try measuring periods in Ticks and use 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);
Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215
  • Exactly, this is the plan. Frequency is sure better than some DateTime member, so +1. Thank you – Mare Infinitus Jul 19 '13 at 11:23
  • 1
    @DmitryBychanko Can you explain more about the +0.4999 in your calculation please? – Mare Infinitus Jul 19 '13 at 11:30
  • 1
    I've round up Double to nearest Int microseconds; e.g. if I've got, say, 3.87 microseconds if I put (int) (3.87) it'll return 3 that's not a desired value. Often we add simply 0.5, but I prefer 0.5 being round up to zero not to one, that's why I put 0.499999... – Dmitry Bychenko Jul 19 '13 at 11:34
1

StopWatch is the thing you need. Use:

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

StopWathch.ElapsedMilliseconds is defined as long. Therefore it is not possible that it is more precise than one millisecond.

Manuel Amstutz
  • 1,311
  • 13
  • 33
1

C# time in microseconds

long microseconds = ticks / (TimeSpan.TicksPerMillisecond / 1000);
Community
  • 1
  • 1
VikciaR
  • 3,324
  • 20
  • 32
1

forgive my ignorance, (but) if it's less than a millisecond (1000 ticks), you surely don't have to diagnose it for performance issues

sarepta
  • 348
  • 1
  • 3
  • 11
  • I have lots of calls to the function I want to measure. But having to make at least 10.000 to have a good measurement in the tests is "boring". – Mare Infinitus Jul 19 '13 at 11:36
  • @MareInfinitus It's also *effective*. Even if you could accurately measure one call, the variance in different calls means that you ought to make a large number of calls and average the time if you want to have a meaningful approximation of it's duration. – Servy Jul 19 '13 at 14:28
  • As mentioned in another comment, I'm making 2,000,000 calls and calculate the average, but I came up with the idea when I saw 317 ticks and 0 ElapsedMilliseconds – Mare Infinitus Jul 19 '13 at 15:05