How does time command work

command linetime

So when I type time <some_program> I have nice CPU time measurement. But exactly how is this achieved? Also does this have any performance impact on the some_program?

Best Answer

man page of time says

The statistics of time command consist of

(i) the elapsed real time between invocation and termination,

(ii) the user CPU time (the sum of the tms_utime and tms_cutime values in a struct tms as returned by times(2))

(iii) the system CPU time (the sum of the tms_stime and tms_cstime values in a struct tms as returned by times(2)).

You can see this where the time command is implemented in c program.

The total CPU time is the combination of the amount of time the CPU(s) spend performing some action for a program and the amount of time the CPU(s) spend performing system calls for the kernel on the program's behalf. When a program loops through an array, it is accumulating user CPU time. Conversely, when a program executes a system call such as exec or fork, it is accumulating system CPU time.

According to the source code of the GNU implementation of time, most information shown by time is derived from the wait3 system call. On systems that do not have a wait3 call that returns status information, the times system call is used instead.

Related Question