views:

57

answers:

2

Hello guys, I'm testing one code provided from my colleague and I need to measure the time of execution of one routine than performs a context switch (of threads).

What's the best choice to measure the time? I know that is available High Resolution Timers like,

QueryPerformanceCounter
QueryPerformanceFrequency

but how can I translate using that timers to miliseconds or nanoseconds?

+2  A: 
Alex Farber
double d_ms = d * 1000.0;
Alex Farber
not work, is returning 1.0 when the execution methods remains aproches 10seconds
ROCK HEART
I'd guess that the reason it isn't working is that the above code performs an integer division and then casts it to a double. Try `d = ((double)(lEnd.QuadPart - lStart.QuadPart)) / lFreq.QuadPart);` instead and see if that helps.
torak
torak - thanks for this fix.
Alex Farber
http://www.codeguru.com/forum/showthread.php?t=291294 Actually, it is possible to use LowPart instead of QuadPart - 32 bit is quite enough.
Alex Farber
A: 

As the operation than i am executing is in order of 500 nanos, and the timers doens't have precision, what i made was,

i saved actual time with GetTickCount() - (Uses precision of ~ 12milis) and performs the execution of a route N_TIMES (Number of times than routine executed) than remains until i press something on console.

Calculate the time again, and make the difference dividing by N_TIMES, something like that:

int static counter;

void routine() { // Operations here.. counter++; }

int main(){
start <- GetTickCount(); handle <- createThread(....., routine,...);
resumeThread(handle);

getchar();

WaitForSingleObject(handle, INFINITE);

Elapsed = (GetTickCount() - start) * 1000000.0) / counter; printf("Nanos: %d", elapsed); }

:)

ROCK HEART