Alex Rivera | Logout

C - measuring computing time

Asked 2011-04-13T05:17:39.787
10

is there any simple way how to measure computing time in C? I tried time utility when executed, but I need to measure specific part of a program.

Thanks

Edit
Report

1 Answer

19

You can use the clock function in <time.h> along with the macro CLOCKS_PER_SEC:

clock_t start = clock() ;
do_some_work() ;
clock_t end = clock() ;
double elapsed_time = (end-start)/(double)CLOCKS_PER_SEC ;

Now elapsed_time holds the time it took to call do_some_work, in fractional seconds.

answered 2011-04-13T05:24:04.597

Your Answer