Search This Blog

Wednesday, August 24, 2011

C++ High Resolution Timer

i used to get timing via GetTickCount API , but the smallest measurement i managed to get was about ~15 ms.
so if you need to measure elapsed time in micro seconds , the next article is for you
High Resolution Timer:


LARGE_INTEGER frequency;        // ticks per second
    LARGE_INTEGER t1, t2;           // ticks
    double elapsedTime;

    // get ticks per second
    QueryPerformanceFrequency(&frequency);

    // start timer
    QueryPerformanceCounter(&t1);

    // do something
    ...

    // stop timer
    QueryPerformanceCounter(&t2);

    // compute and print the elapsed time in millisec
    elapsedTime = (t2.QuadPart - t1.QuadPart) * 1000.0 / frequency.QuadPart;
    cout << elapsedTime << " ms.\n";

No comments: