Groups | Blog | Home
all groups > dotnet performance > november 2003 >

dotnet performance : AverageTimer32


Michael
11/20/2003 3:25:17 PM
All,

I am not sure if this is already a known issue.
When I calculate the time elapse for a partular activity
using AverageTimer32 type perfmon counter, should I use:

long t1=DateTime.Now.Ticks;<--1
//some activities
long t2=DateTime.Now.Ticks;<--2
MyAverageTimer32.incrementBy(t2-t1);
MyBase.increment();

or replace the code in #1, #2 with this

QueryPerformanceCounter(ref t1);
QueryPerformanceCounter(ref t2);

Saw some posts said the first one is not accurate, and
some say we should not use the second style,,, any one
can provide a anwser?

Thanks;

Jon Skeet [C# MVP]
11/21/2003 7:53:16 AM
[quoted text, click to view]

I personally use the first version - it's more portable (to Mono etc)
and doesn't require any extra declarations etc.

Yes, it's less accurate - but I usually reckon that if you're measuring
times so small that the loss of accuracy there is relevant, you're
leaving yourself open to other things (such as other processes getting
timeslices) mucking your results up significantly anyway. Certainly for
benchmarking etc, I prefer to time as long a period as realistically
possible - for small tests, a minute is a good thing to aim for,
although for some types of tests it's unrealistic as other resources
(eg memory) will be exhausted before you can run for a minute.

Anyway, with a period of time like that, the inaccuracy of DateTime.Now
really doesn't matter.

Small point - I like to keep my DateTimes as DateTimes:

DateTime start = DateTime.Now;
// ...
DateTime end = DateTime.Now;
TimeSpan timeTaken = end-start;

--
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet
Michael
11/21/2003 1:10:54 PM
Jon,

Thank you very much for this. One of the things we try to
track is how long it take for a particualr call to a
third-party component, that's why we would like to see
the accurate time of the call. We don't know what happens
behind their component.

Appreciate a lot for your input.

Mike
[quoted text, click to view]
Jon Skeet [C# MVP]
11/22/2003 7:57:50 AM
[quoted text, click to view]

But the best way of finding that out (if you can) is to make that call
a billion times, and measure that - then it doesn't matter if the
measurements are slightly out, because the error *per call* will be
tiny.

--
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet
Jason Dorie
11/25/2003 5:22:28 PM
Which is a wonderful thing to do if you're sure that the 3rd party control
isn't doing anything internally to cache multiple successive calls. I've
found the granularity of the DateTime methods to be about 15ms, which is far
too long for some things.

I use timers to measure GUI responsiveness, and if one call takes 10ms and
another takes only 5, it's below the threshold of DateTime, but still coarse
enough to be useful.

There's no substitute for long-duration timing, but there's also no
substitute for an accurate timer. :-)

Jason Dorie



[quoted text, click to view]

AddThis Social Bookmark Button