Timing en C++ sous GNU/Linux

Mesurer le temps d’exécution d’une portion de code sous GNU/Linux en C++.

#include <iostream>
#include <stdlib.h>
#include <sys/time.h>
#include <cstdlib>

int main() { struct timeval start, end;

long mtime, seconds, useconds;

gettimeofday(&start, NULL); for(int i=10000000; –i>0;) { int a = rand(); int b = rand(); a/=b; } gettimeofday(&end, NULL);

seconds = end.tv_sec - start.tv_sec; useconds = end.tv_usec - start.tv_usec;

mtime = ((seconds) * 1000 + useconds/1000.0) + 0.5; std::cout << “(int) Elapsed time: “ << mtime << “ ms\n”;

return EXIT_SUCCESS; }