This lets you measure the CPU time taken to execute a function in a generic way. The time returned is in microseconds. Thanks to @cswl for showing me the way of measuring CPU time.
This suite is available as a shared library. You can compile the library using make lib
. This will generate libbenchmark.so
. You can then link against this library with your own executable.
- In your executable source, include the library header file by
#include "benchmark.h"
. - If you haven't built the lib already, build using
make lib
from terminal. - Define your (long running?) function, so that it can be invoked from your executable source.
- If the function you want to test, has parameters, wrap it up in a void function with no parameters. In that function, call the required long running function and supply appropriate parameters.
- Then create
test_t
struct, as follows:
test_t sample_test {
.name = "Name of the test",
.function = long_running_function_wrapper,
};
- And then run the test by:
run_benchmark(&sample_test);
Check the test.c to get a brief idea on how to use this.