Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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?

share|improve this question

2 Answers 2

up vote 4 down vote accepted

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.

share|improve this answer

The kernel keeps track of CPU usage statistics for all processes; time just asks the kernel for the information about its child process, which happens to be the command you asked it to run. Since the kernel is keeping track of this information anyway (needed for scheduling, implementation of various resource quotas, etc.), using the time command doesn't make your command run any slower.

share|improve this answer
    
I wonder though whether some_program is a child process of time - by invocation of command it seems like so - time is invoked and some_program passed as a parameter. –  ren Jan 24 '12 at 14:51

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.