time measures better resolution

  • Thread starter Valéry Bezençon
  • Start date
V

Valéry Bezençon

Hello,

I would like to measure the time taken by a method to run for performance
tests.

I use the class DateTime and TimeSpan like this:

start = DateTime.Now;
....method execution
ts = DateTime.Now.Substract(start);

ts is of type TimeSpan. I can get the time using
ts.TotalMilisecond;
However the measures are not precise as the resolution, refering to the .NET
doc, is:

System Approximate Resolution
Windows NT 3.5 and later 10 milliseconds
Windows NT 3.1 16 milliseconds
Windows 98 55 milliseconds


Is it possible to have better resolutions, like 1 millisecond (I have
Windows 2000)?

Thanks
Valéry
 
P

Philip Rieck

You can try the PerformanceCounter class:

http://msdn.microsoft.com/library/d...emdiagnosticsperformancecounterclasstopic.asp


Or you can use interop to call QueryPerformanceCounter:
(there are some articles about this on codeproject, but I'm unable to access
that site right now. Try searching there)

[System.Runtime.InteropServices.DllImport("KERNEL32")]
private static extern bool QueryPerformanceCounter( ref long
lpPerformanceCount);

[System.Runtime.InteropServices.DllImport("KERNEL32")]
private static extern bool QueryPerformanceFrequency( ref long
lpFrequency);
 

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