Why did my c program take longer than its own time?

I am using the time command on linux to determine how much time has elapsed in my program and in my code I have put timers to calculate the 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 longer than calculated in real time, how to find the cause and solve it?

=============================================== === ====

this is how I calculate the time in my code;

clock_t cl;
cl = clock();


cl = clock() - cl;
float seconds = 1.0 * cl / CLOCKS_PER_SEC;
printf("time took: %.3f sec\n", seconds);



source to share

1 answer

There is always overhead to start a process, runtime startup, program shutdown, and time, which probably has overhead as well.

Also, on a multiprocessor operating system, your process can be "disabled", which means that other processes are running while your computer is suspended. It can also mess up the timings.

Let me explain the timing output:

  • real

    means the actual time of the clock, including all service data.
  • user

    is the time spent in a real program.
  • sys

    is the time spent by the kernel system (like the shutdown I talked about earlier)

Please note that user + sys

is very close to your time: 1m7.607s + 0m3.785s == 71.392s


Finally, how did you calculate the time? Without this information, it is difficult to pinpoint exactly what the problem is (if any).



All Articles