Unfortunately there is no way to do this using ANSI C. However, you can do it using gettimeofday
, a POSIX function:
#include <sys/time.h>
struct timeval tv;
struct timeval start_tv;
gettimeofday(&start_tv, NULL);
// call your function here
double elapsed = 0.0;
gettimeofday(&tv, NULL);
elapsed = (tv.tv_sec - start_tv.tv_sec) +
(tv.tv_usec - start_tv.tv_usec) / 1000000.0;
Alternatively, if you want the execution time of your entire program, you could also just run time ./your_program
on the command line.
Finally, if you are on Windows, you can use the timeGetTime
function.