How to measure time spans in .NET?

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
 
G

Guest

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
 
C

Carl Daniel [VC++ MVP]

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
 
W

William DePalo [MVP VC++]

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top