- Linux time Command | Explained
- What is the time Command in Linux?
- How to Use the time Command in Linux?
- Example 1: Time Consumed by a Specific Command
- Example 2: How to Get POSIX Format Time?
- Example 3: How to Get All the Time Details for a Command?
- Example 4: How to Format the Execution Time in Linux?
- Format the Time Using the /usr/bin/time Command
- Example 4.1: How to Format the Real, User, and System Time?
- Example 4.2: How to Format the Real Time Only?
- Example 4.3: How to Format the User Consumed (CPU) Time?
- Example 4.4: How to Save the Execution Time of a Command in a File?
- Conclusion
- Why real time can be lower than user time
- 4 Answers 4
- How to get linux `time` (real, user, sys) for current process in CPP?
- 1 Answer 1
Linux time Command | Explained
Time management in Linux is a key parameter for executing commands and managing the system’s performance. It shows the time of the system, which indicates the system’s speed and time taken by a specific command. The time command runs the programs and summarizes the time for the system resource usage. This guide will give a better understanding of this command using the following topics:
- What is the time Command in Linux?
- General Syntax
- Options for the time Command
- Example 1: Time Consumed by a Specific Command
- Example 2: How to Get POSIX Format Time?
- Example 3: How to Get All the Time Details for a Command?
- Example 4: How to Format the Execution Time in Linux?
- Example 4.1: How to Format the Real, User, and System Time?
- Example 4.2: How to Format the Real Time Only?
- Example 4.3: How to Format the User Consumed (CPU) Time?
- Example 4.4: How to Save the Execution Time of a Command in a File?
Now let’s start discussing every point in detail from the next section
What is the time Command in Linux?
The time command is used to get the real execution time, the user CPU processing time, and the system CPU processing time. This command tells us the time it will be taken for a command to execute, which helps test the performance of the commands and scripts.
The General Syntax of the time command is as follows:
The components of the syntax are described below:
- time: Indicates the time command.
- option: Replace with built-in options of time command.
- command: Write your desired command for pausing.
Options for time Command
The time command has several built-in options which can be utilized by using the options with the command. The available options are written below:
-o This option overwrites the file’s content and destroys the file’s previous content. -a Append the information with the output. This command is used with the “o” option. -f Formats the output for time command. -p This option gives us the PSIOX output. –quiet This option does not show anything in the output. If you want a detailed idea of the options and syntax for the “time” command, use the below command:
Let’s get into the usage of the time command in Linux.
How to Use the time Command in Linux?
We can measure the execution time by prefixing the time with any command. To get the time for the “ls -l” (list the file in a directory) command, use:
The output shows three times consumed for running the command real, user, and sys time. Let’s understand these times!
- Real Time: This time is the time (Wall-clock time) consumed by the system CPU from pressing the “Enter” button to executing the command. The above result shows the real time of 0.014 seconds or 14 milliseconds.
- User Time: The time in seconds used by the CPU in user mode.
- System Time: System time is taken by Kernel to process the command.
Similarly, you can get the time taken by a process to copy one file’s content to another.
Example 1: Time Consumed by a Specific Command
To find the time consumed by copying the testfile1.txt to the testfilefile2.txt, execute the below command:
$ time cp testfile1.txt testfile2.txt
Example 2: How to Get POSIX Format Time?
POSIX is a standard format created by IEEE for smooth compatibility between different operating systems. To get the POSIX formatted time for “sudo apt update”, use the following command:
The real execution time is 5.02 seconds, and the system consumed time is 0.02 seconds.
To check the difference between the above two commands (time and time -p), the below output is provided, clearly showing the different formats for both commands:
$ time sudo apt update $ time -p sudo apt update
Example 3: How to Get All the Time Details for a Command?
The normal time command shows real, user, and system times only, so if you want to display all the time related to the system, use the “/usr/bin/time” directory with the “v” flag of the time command. To get all the times for executing the pwd (print working directory) command, use:
It can be seen that the user time and system time are 0.00, whereas the elapsed wall clock shows the time taken by this to execute the command.
Example 4: How to Format the Execution Time in Linux?
By default, the time in Linux is formatted as hhmmss (for example, 2h13m9s), where h represents the hours, m for minutes, and s for seconds. Usually, the executions do not take hours to complete, so that you would see the time format as 3m2.16s.
To format the time in Linux, we use the /usr/bin/time file, where all the time-related command utilities are present. Let’s check the options for formatting the time Linux:
% Shows a command literal %E To show the real-time %U For user %S For system %C Comand line arguments and name. %I File inputs for the process. %P Percentage CPU, which completes this process. %r Socket messages received by the system. x Exit the command. Let’s get into the details of these options.
Format the Time Using the /usr/bin/time Command
The directory /usr/bin/time has details about the time in Linux. To use the time command according to a specific format, we can use the /usr/bin/time directory as follows:
Let’s discuss the different working examples for this command.
Example 4.1: How to Format the Real, User, and System Time?
We can format the Real-time, User time, and System time using the /usr/bin/time file. If you want to show all the built-in times on the output for the “sudo apt update” command, use the below command utility:
$ /usr/bin/time -f "%E real,%U user,%S sys" sudo apt update
- f: Shows the format option.
- %E: Represents Real-time
- %U: Represents User time
- %S: Represents System time
The output shows the formatted time output with all three system times.
Example 4.2: How to Format the Real Time Only?
If you are running a command and require the Real-time only, you can format it by just using the “%E” option. For instance: to get only the real-time for the “sudo apt update”, execute the following command:
$ /usr/bin/time -f "%E real" sudo apt update
Example 4.3: How to Format the User Consumed (CPU) Time?
While executing the command, the time utilized by the User CPU (outside the Kernel) is User time. To get the User time only for “sudo apt update”, run the below command:
$ /usr/bin/time -f "%U User" sudo apt update
Example 4.4: How to Save the Execution Time of a Command in a File?
Sometimes, we need to save the execution times of the system to keep the performance record of the system. The /usr/bin/time allows us to put the time in a file named testfile3, using the below command:
Note: The “o” flag of /usr/bin/time command outputs the time into a file rather than the terminal.
$ /usr/bin/time -o testfile3.txt sudo apt update
End of this informative guide!
Conclusion
Linux time command is used to get the execution time, including real-time consumed by the system, user mode time, and system mode time. The time can be formatted to multiple formats using the available options in the time command and /usr/bin/time command. This blog post has covered the working and usage of the time command in Linux.
Why real time can be lower than user time
I have a script converting video files and I run it at server on test data and measure its time by time . In result I saw:
real 2m48.326s user 6m57.498s sys 0m3.120s
Why real time is that much lower than user time? Does this have any connection with multithreading? Or what else? Edit: And I think that script was running circa 2m48s
re your EDIT — that makes perfect sense, since real time is wall-clock time as explained below (ie what we would measure if we had a stop-watch)
4 Answers 4
The output you show is a bit odd, since real time would usually be bigger than the other two.
- Real time is wall clock time. (what we could measure with a stopwatch)
- User time is the amount of time spend in user-mode within the process
- Sys is the CPU time spend in the kernel within the process.
So I suppose if the work was done by several processors concurrently, the CPU time would be higher than the elapsed wall clock time.
Was this a concurrent/multi-threaded/parallel type of application?
Just as an example, this is what I get on my Linux system when I issue the time find . command. As expected the elapsed real time is much larger than the others on this single user/single core process.
real 0m5.231s user 0m0.072s sys 0m0.088s
- real < user: The process is CPU bound and takes advantage of parallel execution on multiple cores/CPUs.
- real ≈ user: The process is CPU bound and takes no advantage of parallel exeuction.
- real > user: The process is I/O bound. Execution on multiple cores would be of little to no advantage.
How to get linux `time` (real, user, sys) for current process in CPP?
In cpp, to calculate the elapsed real, user and sys time between a given block of code. WE can assume this is non-portable, an a linux/posix supported system. Which timer functions (CPP11+) can be used for this? I’ve evaluated boost, chrono, ctime, time but could not find the user/sys split anywhere. How can I grab the above data in CPP?
times( returns data with resolution of clock ticks, which can be retrieved with sysconf(_SC_CLK_TCK) and is only 100, meaning the resolution of time from times is 10ms. I am looking for atleast 1ms resolution as reported by the linux time . Are there any other interfaces/timers that offer better resolution for this time? @Homer512 the resolution on that is only 10ms, which seems very coarse. Any ideas on higher resolution calls?
1 Answer 1
On Linux, getrusage is the call that gets user and system time for a process (along with a bunch of other data). See man getrusage for details:
NAME getrusage - get resource usage SYNOPSIS #include #include int getrusage(int who, struct rusage *usage); DESCRIPTION getrusage() returns resource usage measures for who, which can be one of the following: RUSAGE_SELF Return resource usage statistics for the calling process, which is the sum of resources used by all threads in the process. RUSAGE_CHILDREN Return resource usage statistics for all children of the calling process that have terminated and been waited for. These statistics will include the resources used by grandchildren, and further removed descendants, if all of the intervening descendants waited on their terminated children. RUSAGE_THREAD (since Linux 2.6.26) Return resource usage statistics for the calling thread. The _GNU_SOURCE feature test macro must be defined (before including any header file) in order to obtain the definition of this constant from . The resource usages are returned in the structure pointed to by usage, which has the fol‐ lowing form: struct rusage < struct timeval ru_utime; /* user CPU time used */ struct timeval ru_stime; /* system CPU time used */ long ru_maxrss; /* maximum resident set size */ long ru_ixrss; /* integral shared memory size */ long ru_idrss; /* integral unshared data size */ long ru_isrss; /* integral unshared stack size */ long ru_minflt; /* page reclaims (soft page faults) */ long ru_majflt; /* page faults (hard page faults) */ long ru_nswap; /* swaps */ long ru_inblock; /* block input operations */ long ru_oublock; /* block output operations */ long ru_msgsnd; /* IPC messages sent */ long ru_msgrcv; /* IPC messages received */ long ru_nsignals; /* signals received */ long ru_nvcsw; /* voluntary context switches */ long ru_nivcsw; /* involuntary context switches */ >;