Timing Linux Programs

Timing a Linux program helps one to understand how much time was spent.

The versatile Linuxtimecommand can be used for this.

Thetimecommand measures real (i.e.

The main page of the Homepage homelab dashboard with services running on it.

wall clock time), user, and sys time.

The sys time is the time the program runs inside the kernel.

Knowing this, we can accurately measure how much effective CPU time was used (by combining them).

Article image

An example can be seen when timingsleep:

Here we timed thesleepcommand using thetimetool.

As a simple example, we could evaluate if any file system cache is operating on our system.

This time the result came back a little quicker, with 1.255 seconds saved on the command.

A simple time command testing the time taken by a sleep 1 command at the Linux command prompt

Likely a physical disk-based cache helped here.

Then, an alternative command can be used.

In Linux (or more specifically in Bash coding etc.

Listing the contents of /usr with find and timing the same

The command will obtain a process list and display the second column.

We use bothawkandsedto do so, and run each command 1000 times to see the difference in performance overall.

Wrapping up

In this article, we explored the Linuxtimecommand.

Clearing the inode (and other) Linux caches and re-executing our find command in /usr

We clarified whatreal,userandsystimes indicate and how the latter two relate to CPU usage.

We also reviewed several examples of how to usetimein practical ways.

Can it be Fixed?.

Re-executing the find command without clearing the caches results in much faster execution

Using time to analyze the average execution time of any command, allowing one to optimize Linux script runtimes