Home Learn Linux Understanding and applying the time command in Linux

Understanding and applying the time command in Linux

This article offers a detailed exploration of the Linux time command, an essential tool for developers and system administrators to measure the execution time of commands and scripts. Learn how to effectively use time, interpret its output, and apply it to optimize performance with practical examples.

by John Horan
time command in linux

If you’ve ever found yourself lost in the depths of a Linux terminal, wondering how long a certain command or script takes to execute, then you’re in the right place. Today, we’re digging deep into one of Linux’s nifty little tools, the time command. This command, while simple in nature, holds a special place in my heart for its utility and straightforwardness.

What is the time command?

In Linux, the time command is used to measure the duration a particular command or script takes to execute. It provides three critical pieces of information: the real time, user CPU time, and system CPU time. Let’s break these down:

  • Real time: This is the actual elapsed time from start to finish of the command’s execution, often referred to as wall-clock time.
  • User CPU time: The time the CPU spends in user mode, executing user processes.
  • System CPU time: The time the CPU spends in kernel mode, executing system calls on behalf of the processes.

I personally love using time for quick performance checks or to optimize scripts by comparing different approaches to solving the same problem.

How to use the time command

Using time is as simple as prefixing it to any command or script you want to measure. Here’s the basic syntax:

time [options] command [arguments...]

Now, let’s dive into some examples.

Basic usage

Suppose you have a script named script.sh and you want to know how long it takes to run. Simply use:

time ./script.sh

The output might look something like this:

real    0m1.003s
user    0m0.000s
system  0m0.002s

Here, the script took just over 1 second to complete (real), with virtually no time spent in user or system CPU processes. I find this breakdown incredibly useful for understanding where the time is going, especially for more complex scripts.

Using time with built-in shell commands

One thing to note, especially for bash users, is that time behaves slightly differently with built-in shell commands. To ensure you’re using the external time command and not the shell built-in, you might need to specify the full path or use a different syntax. Here’s an example using the echo command:

/usr/bin/time echo "Hello, World!"

Or, you can use:

time -p echo "Hello, World!"

The -p option makes time use a portable output format, which is more widely compatible.

Advanced usage: Formatting the output

For those who, like me, love diving into the nitty-gritty details, time allows you to format the output. This is particularly useful when you’re only interested in specific metrics. The -f or --format option is your friend here. You can use predefined format strings to display the information that matters to you.

For example, to display just the real time in seconds:

time -f "%e seconds" ./script.sh

This might output something like:

1.00 seconds

I find this level of customization incredibly powerful for scripting and logging performance metrics.

Combining time with other commands using pipes and redirections

One might not think of time as a command that plays well with pipes and redirections due to its nature of measuring execution time. However, with a bit of creativity, you can indeed combine it with other commands. For instance, if you want to measure the time taken by a pipeline of commands, you can wrap the entire pipeline in a shell invocation:

time sh -c 'grep "search_term" large_file.txt | sort > sorted_results.txt'

This will output the time taken to grep through a large file and sort the results. A sample output could look like this:

real    0m2.157s
user    0m0.632s
system  0m0.124s

This tells us the entire pipeline took a little over 2 seconds, with most of the time spent in user mode.

Profiling with verbose output

For those who thirst for more detailed information, time offers a verbose mode through the -v or --verbose option. This mode provides a wealth of information beyond the basic real, user, and system times.

Let’s say we have a more complex script, data_process.sh, which involves reading, processing, and writing data. Using verbose mode can give us insights into the script’s resource usage:

/usr/bin/time -v ./data_process.sh

The output might include details like memory usage, context switches, and file system inputs/outputs, resembling:

        Command being timed: "./data_process.sh"
        User time (seconds): 0.02
        System time (seconds): 0.01
        Percent of CPU this job got: 97%
        Elapsed (wall clock) time (h:mm:ss or m:ss): 0m 0.03s
        Average shared text size (kbytes): 0
        Average unshared data size (kbytes): 0
        ...
        Maximum resident set size (kbytes): 3720
        ...
        Minor (reclaiming a frame) page faults: 304
        Voluntary context switches: 10
        Involuntary context switches: 5
        ...

This verbose output can be particularly enlightening when diagnosing performance issues or when tuning the efficiency of your scripts.

Using time for benchmarking

Time can also be an invaluable tool for simple benchmarking tasks. Let’s say you’re experimenting with different algorithms or configurations in your scripts. By using time, you can get a quantifiable measure of each variation’s performance.

For example, if you’re testing two different sorting algorithms implemented in sort1.sh and sort2.sh, you can run:

time ./sort1.sh
time ./sort2.sh

And compare the outputs to see which algorithm is faster. This can be incredibly useful for optimizing performance-critical applications.

Real-world example: Compiling a large program

A practical example where time is invaluable is in compiling large programs. Compiling can be time-consuming, and using time can help you understand how long the process takes and where the time is being spent.

time make

A sample output might look like this:

real    1m15.342s
user    0m55.500s
system  0m19.832s

This output reveals that the real time to compile the program was over a minute, with the majority of time spent in user mode. This information can be useful for developers looking to optimize their build processes or for system administrators trying to diagnose build server performance issues.

Wrapping up

The time command, with its simplicity and versatility, is a powerful tool in the Linux arsenal. From basic execution time measurements to detailed performance profiling and benchmarking, time offers a range of functionalities suited for various scenarios.

The examples and advanced uses discussed here are just the tip of the iceberg. The Linux ecosystem is vast and full of possibilities, and tools like time are your allies in navigating and mastering it. So, go ahead and experiment, explore, and most importantly, have fun timing!

You may also like

Leave a Comment

fl_logo_v3_footer

ENHANCE YOUR LINUX EXPERIENCE.



FOSS Linux is a leading resource for Linux enthusiasts and professionals alike. With a focus on providing the best Linux tutorials, open-source apps, news, and reviews written by team of expert authors. FOSS Linux is the go-to source for all things Linux.

Whether you’re a beginner or an experienced user, FOSS Linux has something for everyone.

Follow Us

Subscribe

©2016-2023 FOSS LINUX

A PART OF VIBRANT LEAF MEDIA COMPANY.

ALL RIGHTS RESERVED.

“Linux” is the registered trademark by Linus Torvalds in the U.S. and other countries.