Looking for benchmarking code snippet (c++)

Mizipzor picture Mizipzor · Jan 27, 2009 · Viewed 20.6k times · Source

Some loading routines in my program takes to long to complete. I want a quick small snippet for checking how long a function took to execute. By small I mean "preferably without 3rd party libraries".

Maybe something as simple as taking the system time?

start = current_system_time()
load_something()
delta = current_system_time()-start
log_debug("load took "+delta)

Edit: Target OS in question is Windows.

Answer

Robert Gould picture Robert Gould · Jan 27, 2009

Your answer: Yes

Caveat: That WON'T work in multihtreaded code or multiple core machines, you need a robust wall-clock timer. So I recommend you use omp's wallclock. OMP is included with VC and GCC, and most compilers and its a standard you don't need to worry about disappearing

#include <omp.h>

// Starting the time measurement
double start = omp_get_wtime();
// Computations to be measured
...
// Measuring the elapsed time
double end = omp_get_wtime();
// Time calculation (in seconds)