How to measure time spans in .NET?

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I used the QueryPerformanceCounter and QueryPerformanceFrequency methods in
my VC++ 6.0 projects to measure correct time spans, for example for timeouts,
how much time takes to execute certain method etc

I wonder whether there is something similar in .NET to measure time spans?

Meantime I still use these methods through DllImport
[DllImport("Kernel32.dll")]
private static extern bool QueryPerformanceFrequency(out long lpFrequency);

I suspect there is better approach then using DllImport. Can anyone suggest
something better?

Thanks
 
Steve said:
I used the QueryPerformanceCounter and QueryPerformanceFrequency methods in
my VC++ 6.0 projects to measure correct time spans, for example for timeouts,
how much time takes to execute certain method etc

I wonder whether there is something similar in .NET to measure time spans?

Meantime I still use these methods through DllImport
[DllImport("Kernel32.dll")]
private static extern bool QueryPerformanceFrequency(out long lpFrequency);

I suspect there is better approach then using DllImport. Can anyone suggest
something better?

Thanks

Actually, that if you want high precision timing, I still recommend using
explicit Pinvoke the way you describe. In .NET you have access to
System.DateTime structure which is stated as 100 nanoseconds (1 / 10
millionth of a second). If that precision is good enough you should be able
to use that instead.

Thanks,
Kapil
 
Kapil said:
Actually, that if you want high precision timing, I still recommend
using explicit Pinvoke the way you describe. In .NET you have access
to System.DateTime structure which is stated as 100 nanoseconds (1 /
10 millionth of a second). If that precision is good enough you
should be able to use that instead.

Just to be clear: DateTime has 100 nanosecond resolution. It does not, by
any stretch have 100 nanosecond accuracy or precision. Rather, the
precision and accuracy are on the order of +/- 5-7ms (10-15ms tick time).

-cd
 
Carl Daniel said:
Just to be clear: DateTime has 100 nanosecond resolution. It does not,
by any stretch have 100 nanosecond accuracy or precision. Rather, the
precision and accuracy are on the order of +/- 5-7ms (10-15ms tick time).

Absolutely right.

FWIW: while there are no guarantees as to minimum latencies on any version
of Windows (except _maybe_ CE, I dunno) you can get the precison down as low
as it will go by bracketing time sensitive sections of code with

timeBeginPeriod(1);

and

timeEndPeriod(1);

It's not at all obvious, but things like the smallest period you can Sleep()
change in the presence of those two inoccuous looking lines.

Regards,
Will
 
Back
Top