I was thinking of something like:
// Runs algorithm() n times during an amount of seconds.
void run(int seconds) {
clock_t start = clock();
clock_t current = start;
while(double (current - start) / CLOCKS_PER_SEC <= seconds) {
algorithm();
current = clock();
}
}
I chose clock() over time() to avoid accounting time when the process sleeps.
I would like to know if there is a better way to achieve this, without using chrono.
STLSoft has a performance_counter which works on UNIX and windows platforms. The library is header only and only requires a simple include:
You could also look at the source for hints on making your own.