High Resolution Timer

Download: timer.zip

Overview

C standard library provides clock() function, which can be used to measure the elapsed time. It is a system independent C function declared in time.h (compatible on most operating systems), but it does not give accurate results, not even millisecond accuracy.

In order to see the accuracy of clock(), try the following code on your system. The output tells the minimum clock difference which clock() function can detect. I got about 15 ms resolution on my system. Therefore, we need a high resolution timer to measure the elapsed time at least 1 milli-second accuracy.


#include <iostream>
#include <ctime>
using namespace std;

int main()
{
    clock_t t1, t2;
    t1 = t2 = clock();

    // loop until t2 gets a different value
    while(t1 == t2)
        t2 = clock();

    // print resolution of clock()
    cout << (double)(t2 - t1) / CLOCKS_PER_SEC * 1000 << " ms.\n";

    return 0;
}

std::chrono

C++ standard introduced std::chrono library in C++11 with higher clock resolution upto 1 nanosecond. The advantage of std::chrono is that it can be used for cross-platform because it is a C++ standard library.

std::chrono library consists of 3 main types of classes. The first type is clock itself; std::system_clock, std::steady_clock or std::high_resolution_clock. The second type is the time point class, chrono::time_point to represent the time passed since the reference epoch. Finally, the third type is the duration class, chrono::duration to represent the number of ticks in a time interval. chrono::duration::count() will return the number of ticks with the given time unit.

The following code is a usage of std::chrono to measure the elapsed time.


#include <chrono>
#include <iostream>
using namespace std;

int main()
{
    // start timer with high_resolution_clock, 1st time point
    auto tp1 = chrono::high_resolution_clock::now();

    // do something
    ...

    // stop timer, 2nd time point
    auto tp2 = chrono::high_resolution_clock::now();

    // compute delta duration in millisec as double
    chrono::duration<double, std::milli> duration = tp2 - tp1;

    // print the elapsed time
    cout << duration.count() << " ms.\n";

    return 0;
}

To measure the resolution of std::chrono's clock, try the following code. It will return the nano seconds for a single tick.


// nano-sec for 1 tick
chrono::nanoseconds nanoPerTick = chrono::high_resolution_clock::duration(1);
cout << "Resolution: " << nanoPerTick.count() << " ns.\n";

QueryPerformanceCounter() for Windows

Windows API provides an extremely high resolution timer functions, QueryPerformanceCounter() and QueryPerformanceFrequency(). QueryPerformanceCounter() is used to get the current elapsed ticks, and QueryPerformanceFrequency() is used to get the number of ticks per second, which is used to convert ticks to the actual time. This function can measure at least 1 micro-sec difference. Note that it is only available on Windows system.

Here is a usage of QueryPerformanceCounter() for measuring the elapsed time.


#include <iostream>
#include <windows.h>                // for Windows APIs
using namespace std;

int main()
{
    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";

    return 0;
}

gettimeofday() for Unix, Linux and MacOS

gettimeofday() can be used in Unix or Linux based system. This function is declared in "sys/time.h", so you must include this header before using gettimeofday(). It also produces 1 micro second resolution. Here is a code snippet.


#include <iostream>
#include <sys/time.h>                // for gettimeofday()
using namespace std;

int main()
{
    timeval t1, t2;
    double elapsedTime;

    // start timer
    gettimeofday(&t1, NULL);

    // do something
    ...

    // stop timer
    gettimeofday(&t2, NULL);

    // compute and print the elapsed time in millisec
    elapsedTime = (t2.tv_sec - t1.tv_sec) * 1000.0;      // sec to ms
    elapsedTime += (t2.tv_usec - t1.tv_usec) / 1000.0;   // us to ms
    cout << elapsedTime << " ms.\n";

    return 0;
}

C++ Timer Class

This C++ timer class is a utility class to measure the elapsed time using std::chrono. Therefore, it can be used on both Unix/Linux/MacOS and Windows system. Also, it provides simple interfaces to get the elapsed time easily, for example, getElapsedTimeInSec(), getElapsedTimeInMilliSec(), getElapsedTimeInMicroSec(), etc.
The source is available here: timer.zip

The following code shows the basic usage.


#include <iostream>
#include "Timer.h"
using namespace std;

int main()
{
    Timer timer;

    // start timer
    timer.start();

    // do something
    ...

    // stop timer
    timer.stop();

    // print the elapsed time in millisec
    cout << timer.getElapsedTimeInMilliSec() << " ms.\n";

    return 0;
}
←Back
 
Hide Comments
comments powered by Disqus