16Jul/150
c, c++ measure elapsed time
Elapsed time of function, for performance measurement
C style
#include <sys/time.h> struct timeval begin_v, end_v; gettimeofday(&begin_v, NULL); // the function gettimeofday(&end_v, NULL); const double s = end_v.tv_sec + end_v.tv_usec / 1e6 - begin_v.tv_sec - begin_v.tv_usec / 1e6; printf ("Elasped time: %f s\n", s );
C++ style
#include <chrono> std::chrono::time_point<std::chrono::steady_clock> start, end; start = std::chrono::steady_clock::now(); // the function end = std::chrono::steady_clock::now(); std::chrono::duration<double> elapsed_seconds = end-start; std::cout << "Elapsed time: " << elapsed_seconds.count() << " s" << std::endl;
edit: instead of user-adjustable system_clock
, the monotonic steady_clock
can be a better choice.
Leave a comment