Hi
I have written a C program that is supposed to run in under 2 seconds. Is there a function in C that lets me time how long it takes to run the program?
Thanks
Printable View
Hi
I have written a C program that is supposed to run in under 2 seconds. Is there a function in C that lets me time how long it takes to run the program?
Thanks
I often used clock() in time.h to get the time in millisecs. Only problem with that is that returns the number of clock cycles on linux so you have to divide it by CLOCKS_PER_SEC according to the manpage. Anyway its simple to use:
Alternatively theres gettimeofday(), which is more precise.Code:start_time = clock()
// do stuff
end_time = clock()
seconds_elapsed = (end_time - start_time) / 1000;
On Linux I'd be using time anyway.
Of course that method also counts how long your program takes to start.Code:[ ~]$ time ./myprog
real 0m3.855s
user 0m0.057s
sys 0m0.297s