Archived

This topic is now archived and is closed to further replies.

Yosepp

C# timing

Recommended Posts

Yosepp    122
How could one measure length of time periods in C#. In C++ you cold do it like that:
double ticks = getTickCount();
//Time consuming stuff here

double duration = ticks - getTickCount();
But how would you do it in C#? I tries System.Timers.Timer, but that is for making things happen at regular intervals. Or am i just missing something? Thanks

Share this post


Link to post
Share on other sites
Yosepp    122
First of all, thanks for the fast reply. Now i have two variants:
one is to use what cody suggested, other uses Win32 API in kernel32.dll and QueryPerformanceCounter(). Which should i use?(its for profiling). I've heard that TickCount isn't very precise.

public class Profiler
{
public event FunctionEvent Function;
private long freq,before,after;

[DllImport("kernel32.dll")]
private static extern bool QueryPerformanceCounter(out long lpPerformanceCount);

[DllImport("kernel32.dll")]
private static extern bool QueryPerformanceFrequency(out long lpFrequency);

public Profiler()
{
QueryPerformanceFrequency(out freq);
}

private void Start()
{
QueryPerformanceCounter(out before);

//before = System.Environment.TickCount;

}

private double Stop()
{
QueryPerformanceCounter(out after);
return (double)((after - before) / (double)freq)*1000;

//after = System.Environment.TickCount;


return (double)(after-before);
}

public double Test(long times)
{
Start();
for(long i=0;i<times;i++)
{
Function();
}
return Stop();
}

public delegate void FunctionEvent();
}


Both return a similar value.

[edited by - Yosepp on March 30, 2004 7:11:59 AM]

[edited by - Yosepp on March 30, 2004 7:13:07 AM]

Share this post


Link to post
Share on other sites
joanusdmentia    1060
Just stick with System.Environment.TickCount since it''s native to .NET, especially if you''re not seeing any major difference between the timings you get with each of them.

Share this post


Link to post
Share on other sites
Telamon    157
GetTickCount is garbage. It''s only accurate to ~20ms on XP. The PerformanceTimer functions, however, can be accurate to the nanosecond (neglecting the overhead of the call itself) as it''s update frequency is directly tied into the processor - on my machine it operates at 1.19 Ghz. That''s much better than 50 Hz.

Just my two cents.

----------------------------------------
Let be be finale of seem, seems to me.
----------------------------------------

Coding:
http://www.stanford.edu/~jjshed/coding

Miscellany:
http://www.stanford.edu/~jjshed

Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
If the Profiler class Test routine returns a 10, what is that? 10Mhz? 10ns?

Eric

Share this post


Link to post
Share on other sites
frostburn    380
quote:
Original post by Anonymous Poster
If the Profiler class Test routine returns a 10, what is that? 10Mhz? 10ns?

Eric


Good question. The Stop() Method has 2 returns... I assume it returns the first and then stop (noob). Then it''s 10ms. If it would return the last it''s 10 ticks (which is defined by QueryPerformanceFrequency which could be 500Mhz, 2Ghz or "any" other value - it depends on the machine)

Share this post


Link to post
Share on other sites