Difference Between User-CPU-Time and System-CPU-Time in UNIX
Unix systems have a time utility that allows a user to see where their application took significant time to process. The syntax of this utility is as follows:
time <command-to-be-timed>
Its result generally has three categories as follows:
real <time> user <time> sys <time>
User-CPU-Time:
User-CPU-time is the time taken by the processor to process the code in your application. It is generally termed as running on user-space as opposed to being in the kernel space. When you develop your application, you would have written programming constructs such as conditionals, expressions, looping statements, branches, etc., The time spent on the actual code written by the client is measured via user-cpu-time.
System-CPU-Time:
System-CPU-time is the time taken by the process to process kernel code. When an application is programmed, developers need to write to system output, read from system input, and access a local disk resource such as files, databases, etc., These cross-cutting concerns across applications are addressed via system calls that your operating systems provide. The time spent on this execution is generally referred to as system-cpu-time.
Real:
Real-time is the total time spent beginning from the time on which the application has started and the time on which the application has done its task. It may also include time spent waiting for its turn to process or waiting for resources for the successful execution of the program.
Differences between User-CPU-time and System-CPU-time:
| S. No. | user-cpu-time | system-cpu-time |
|---|---|---|
| 1. | It is the measure of time taken by the application while executing the code written by the user | It is the measure of time taken by an application while executing kernel code |
| 2. | In Unix-based systems, it is generally represented as ‘user’ in response to time utility. | In Unix-based systems, it is generally represented as ‘sys’ in response to time utility. |
| 3. | The time taken can be analyzed and optimized by the user. | Time taken by the system depends on the system calls of the underlying kernel. |
For Example:
#include<stdio.h>
#include<sys/types.h>
#include<stdlib.h>
#include<unistd.h>
int main()
{
int i=0;
sleep(60);
FILE* fpt;
fpt = fopen("/home/test/file1.txt","w");
for(int i=0;i<10000000;i++)
{
printf("");
fprintf(fpt,"%d",i);
}
fclose(fpt);
}The above code first sleeps for a minute, opens a file resource on the disk, and writes the variable “i” to it till the loop terminates and prints nothing to the console. Finally, it closes the file resource opened from the disk.
We can compile this code with:
gcc test.c
This creates a binary ./a.out
Now if we run,
time ./a.out
On UNIX systems it may generate output as seen below. The actual time may vary from time to time based on the program logic and CPU utilization.
Output:
real 1m0.557s user 0m0.452s sys 0m0.084s


