A wrapper around the CXX-11's std::chrono
library with high-precision timer to measure precise
execution times in a program. The wrapper enables direct use in C and C++ programs.
The module is provided with a cmake build system. The compile sequence is:
mkdir build
cd build
cmake .. [-G <Generator>] [-DCMAKE_INSTALL_PREFIX=<install path>]
cmake --build . --config [Release/Debug]
This builds a static library function_timer
that can be installed to the prefix using:
cmake --install .
The library, though written in C++, provides with C and a C++ interfaces.
- Include the header file
function_timer.h
- Create the timer pointer instance
function_timer *timer = new_timer();
- Start timer instance
start_timer(timer);
- End timer & get the duration since timer start.
double duration = end_timer(timer);
If the end_timer
function is called before calling the start_timer
, the
duration value is not guranteed to be 0 and will be invalid. There will not
be any error messages.
- Delete the timer instance
delete_timer(timer);
- Include the header file
function_timer.h
- Create the timer pointer instance
function_timer *timer = new function_timer();
// or
function timer;
- Start timer instance
timer.start_timer();
- End timer & get the duration since timer start.
timer.end_timer();
double duration = timer.get_duration();
If the end_timer
function is called before calling the start_timer
, the
duration value is not guranteed to be 0 and will be invalid. There will not
be any error messages.
- If instantiated as a pointer, delete the timer instance
delete timer; // iff timer is a pointer
Two sample implementations are provided as
-
test.c
: test case in C -
test.cpp
: test case in C++
To compile with C++, must enable the CXX-11 standard to account for std::chrono
library.