Master Linux ‘time’: Measure Script Execution Like a Pro
This guide explains how to use the Linux time command to accurately measure script or command execution duration, covering basic usage, output redirection, detailed statistics, and custom formatting for precise performance testing in time‑critical environments.
Programmers often need to measure how long a script or command runs, especially when dealing with time‑sensitive systems such as NTP synchronization.
The time command provides a simple way to test execution time and obtain real, user, and system durations.
1. Basic usage of time
The most straightforward form is:
$ time ping baidu.com
... (ping output) ...
real 0m11.173s
user 0m0.004s
sys 0m0.002sIn the result, real shows the total elapsed wall‑clock time, while user and sys represent the time spent in user space and kernel space respectively.
2. Writing time information to a file
Use the -o option to direct the timing report to a file:
$ /usr/bin/time -o /home/alvin/time-output.txt ping baidu.comThe command’s normal output still appears on the screen, but the timing data is saved to time-output.txt. To append instead of overwrite, use -a:
$ /usr/bin/time -a /home/smart/time-output.txt ping linoxide.com3. Displaying more detailed timing information
Adding the -v flag produces a verbose report with extensive statistics:
$ /usr/bin/time -v ping baidu.com
... (detailed statistics including CPU usage, memory, page faults, context switches, etc.) ...4. Customizing the output format
The default output shows only real, user, and sys. You can define a custom format with -f and a list of format specifiers (e.g., %E for elapsed time, %I for inputs, %O for outputs):
$ /usr/bin/time -f "Elapsed Time = %E, Inputs %I, Outputs %O" ping baidu.comTo insert line breaks, include \n in the format string:
$ /usr/bin/time -f "Elapsed Time = %E
Inputs %I
Outputs %O" ping baidu.comThis produces output such as:
Elapsed Time = 0:03.92
Inputs 0
Outputs 0By leveraging these options, you can tailor the time command to provide exactly the performance data you need for testing and optimization.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
MaGe Linux Operations
Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
