Timing a function!

Started by
12 comments, last by Kitt3n 16 years, 9 months ago
QueryPerformanceFrequency never changes, so you don't need to call it multiple times.

From http://msdn2.microsoft.com/en-us/library/bb173458.aspx
Quote:Call QueryPerformanceFrequency only once, because the frequency will not change while the system is running.


Also that class casts the result to long which gives you only second precision. It's generally better to either return ms or us as an integral value or use float for seconds.
Advertisement
Quote:Original post by Jerax
QueryPerformanceFrequency never changes, so you don't need to call it multiple times.

From http://msdn2.microsoft.com/en-us/library/bb173458.aspx
Quote:Call QueryPerformanceFrequency only once, because the frequency will not change while the system is running.


This is something I was unsure of - thanks for the clarification. I corrected the code above.
Quote:Original post by Jerax
Also that class casts the result to long which gives you only second precision. It's generally better to either return ms or us as an integral value or use float for seconds.

That's why I divided the frequency by 1000:
frequency.QuadPart /= 1000; // to get milliseconds

[smile]
A µs accuracy cannot be properly achieved in a multiprocess, multithreaded environment - where you have to deal with task swicthed and thread scheduling. Anyway, handling that case is not difficult in the code above (just divide the frequency by 1,000,000 instead of 1,000). Please note that floating point numbers are not accurate either, so you might want to use either 32 bits integers or 64 bit integers (__int64 if you use a 32 bits Visual C++, long long if you use a 32 bits gcc).

Cheers ;)
Thanks so much I will definatly use this class in the future, works great.

I had a bit of trouble implementing it, but that was totally my fault with a really stupid oversight, i couldnt figure out why both print outs gave me the same number.

Im sure you will see the error in seconds

cheers
thanks again

// code shows my previous oversight	// time with a milliseconds accuracy			timer  my_timer(timer::milliseconds);								for(int i = 0; i < 1000; i++)		DrawTriangle1(memDC, BLUE);		my_timer.stop();int ScanFlood =  my_timer.get_accumulated_time();		my_timer.restart();				for(int i = 0; i < 1000; i++)			DrawTriangle(memDC, GREEN);		my_timer.stop();int FillFlood =  my_timer.get_accumulated_time();	//#############################################################################								char str[100];					sprintf(str,"Flood Fill Scan = %d", my_timer.get_accumulated_time() );					TextOut(memDC, 10, 20, str, strlen(str));										char str2[100];					sprintf(str2,"Flood Fill = %d", FillFlood);					TextOut(memDC, 10, 300, str2, strlen(str2));
>QueryPerformanceFrequency never changes, so you don't need to
>call it multiple times.
I'm not sure if that is true - especially with speed-throttling
or and multicore cpu's (where cores might run on different speeds)...


visit my website at www.kalmiya.com

This topic is closed to new replies.

Advertisement