I use time command on linux to measure how long my program took, and in my code I have put timers to calculate time
time took calculated by program: 71.320 sec
real 1m27.268s
user 1m7.607s
sys 0m3.785s
I don’t know why my program took real time more than calculated, how to find the reason and resolve it?
======================================================
here is how I calculate time in my code;
clock_t cl;
cl = clock();
do_some_work();
cl = clock() - cl;
float seconds = 1.0 * cl / CLOCKS_PER_SEC;
printf("time took: %.3f sec\n", seconds);
There always is overhead for starting up the process, starting the runtime, closing the program and time itself probably also has overhead.
On top of that, in a multi-process operating system your process can be “switched-out”, meaning that other processes run while yours in put on hold. This can mess with timings too.
Let me explain the output of time:
realmeans the actual clock time, including all overhead.useris time spent in the actual program.sysis time spent by the kernel system (the switching out I talked about earlier, for example)Note that
user + sysis very close to your time:1m7.607s + 0m3.785s == 71.392s.Finally, how did you calculate the time? Without that information it’s hard to tell exactly what the problem (if any) is.