Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C - measuring computing time

Tags:

c

profiling

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

like image 349
Waypoint Avatar asked Apr 13 '11 05:04

Waypoint


2 Answers

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.

like image 168
Rune Aamodt Avatar answered Oct 10 '22 11:10

Rune Aamodt


You can try the profiler "gprof". More information here: http://www.cs.utah.edu/dept/old/texinfo/as/gprof.html

like image 42
BiGYaN Avatar answered Oct 10 '22 12:10

BiGYaN