plf::nanotimer is a simple C++03/11/etc timer class for ~microsecond-precision cross-platform benchmarking. As a result the implementation is as limited and simple as possible to create the lowest amount of overhead. It was primarily constructed because cross-compiler support for sub-timeslice timer readings using std::chrono was lacking, and boost::chrono does not give sub-timeslice readings on all versions of windows. Also included in the header are some additional 'delay' functions.
plf::nanotimer uses QPC under Windows (see this post for it's resolution and quirks), clock_get_time under Mac OSX, and clock_gettime (with CLOCK_MONOTONIC) under Linux/BSD. Windows XP is supported but see the article above for it's failings under that platform. The timer does not in fact 'count' the time - it takes a measurement when the timer is started, then again when a function to measure the elapsed time has passed. The timer can be 'paused/unpaused' by taking a reading of the current elapsed time when you want to pause, then calling 'start' again when it's time to unpause (this resets the timer). At the next reading of elapsed time, simply add to the previous reading.
plf::nanotimer is under a permissive zlib license. This means: Software is used on 'as-is' basis. It can be modified and used in commercial software. Authors are not liable for any damages arising from its use. The distribution of a modified version of the software is subject to the following restrictions:
Download here (3kb zip file) or view the repository.
plf::nanotimer is a simple .h header-only library, to be used with
an #include command.
nanotimer()
Default constructor - no arguments.
Example: plf::nanotimer timer;
void start()
Start or restart timer.
Example:
timer.start();
// Do something here
double results = timer.get_elapsed_ns();
std::cout << "Timing: " << results << " nanoseconds." << std::endl;
timer.start(); // restart time
// Do something here
// etcetera
Example 2 - "pause" the timer, then "resume":
timer.start(); // start timer
// Do something here
double reading = timer.get_elapsed_ns(); // "Pause" (read) timer
// Do some other stuff - "paused" time
timer.start(); // "Resume" (restart) timer
// Do something here
reading += timer.get_elapsed_ns(); // Add previous reading to current reading to get total reading without "paused" time in middle
double timer.get_elapsed_ns()
Get elapsed time in nanoseconds.
Example:
timer.start();
// Do something here
double results = timer.get_elapsed_ns();
std::cout << "Timing: " << results << " nanoseconds." << std::endl;
double timer.get_elapsed_us()
Get elapsed time in microseconds.
Example:
timer.start();
// Do something here
double results = timer.get_elapsed_us();
std::cout << "Timing: " << results << " microseconds." << std::endl;
double timer.get_elapsed_ms()
Get elapsed time in milliseconds.
Example:
timer.start();
// Do something here
double results = timer.get_elapsed_ms();
std::cout << "Timing: " << results << " milliseconds." << std::endl;
void plf::nanosecond_delay(double x)
Delay until 'x' number of nanoseconds have passed. This is a polling loop with no freeing of timeslices to the OS.
Example:
// Pause program:
plf::nanosecond_delay(1040000);
void plf::microsecond_delay(double x)
Delay until 'x' number of microseconds have passed. This is a polling loop with no freeing of timeslices to the OS.
Example:
// Pause program:
plf::microsecond_delay(10400);
void plf::millisecond_delay(double x)
Delay until 'x' number of milliseconds have passed. This is a polling loop with no freeing of timeslices to the OS.
Example:
// Pause program:
plf::millisecond_delay(104);
Contact:
plf:: library and this page Copyright (c) 2022, Matthew Bentley