Benchmark Commands with time and perf

Summary:
Analyze execution time and performance.


When optimizing code or scripts, it’s essential to measure execution time and performance characteristics to identify bottlenecks and improve efficiency. Two powerful tools for benchmarking on Linux systems are time and perf. This article covers how to use these tools to gain meaningful insights about your applications.

Measuring Execution Time with time

The time command reports how long a program takes to execute. It’s included in most Unix-like systems. When you prefix a command with time, it executes the command and returns timing statistics.

Basic Usage

time <command>

Example:

time ls -l /usr/bin

Output Explained

Typical output looks like this:

real    0m0.012s
user    0m0.004s
sys     0m0.008s
  • real: Actual elapsed wall-clock time (from command start to end).
  • user: CPU time spent in user-mode (actual code).
  • sys: CPU time spent in kernel-mode (system calls, IO).

Note: The time shell keyword and the standalone /usr/bin/time command-line utility provide slightly different options. With the standalone version, you can use -v for verbose output:

/usr/bin/time -v <command>

This adds details like maximum memory usage, page faults, and more.


Analyzing Performance with perf

While time measures elapsed and CPU time, perf is a comprehensive performance analysis tool for Linux, offering detailed insights into CPU cycles, cache usage, and hardware/software events. perf is part of the Linux perf tools suite and is very powerful for profiling.

Installing perf

On Ubuntu/Debian:

sudo apt-get install linux-tools-common linux-tools-generic

On Fedora:

sudo dnf install perf

Check your distribution’s documentation for details.

Basic Usage

To measure overall performance statistics:

perf stat <command>

Example:

perf stat ls -l /usr/bin

Sample Output:

 Performance counter stats for 'ls -l /usr/bin':

         0.019462      task-clock (msec)
              15      context-switches          #    0.771 K/sec
               0      cpu-migrations            #    0.000 K/sec
              73      page-faults               #    3.751 M/sec
         0.003350      cycles                   #    0.172 GHz
         0.003059      instructions             #    0.91  insn per cycle
            ... etc.

Understanding the Output

Some useful metrics:

  • cycles: CPU clock cycles consumed.
  • instructions: Instructions executed (shows efficiency).
  • context-switches: Number of times the CPU context switched (could be a sign of multitasking overhead).
  • cache-misses: (if shown) Cache misses, indicating potential inefficiency.

Recording Profiling Sessions

perf can record detailed profiling data for in-depth analysis:

perf record <command>

After execution, analyze the data:

perf report

This displays a breakdown of where CPU time was spent in your program, such as functions or lines of code, assuming your binary is built with debug symbols.


Comparing time and perf

  • time is simple, lightweight, and gives an immediate sense of elapsed, user, and system time. Great for quick timing checks.
  • perf provides comprehensive, low-level system performance information. Useful for deep dives, spotting CPU and cache issues, or identifying function-level hotspots.

Use time for quick measurements; use perf for investigations when you need to understand why your program runs slowly or how it interacts with system resources.


Example: Benchmarking a Script

Suppose you have a Python script, script.py. Here’s an example benchmarking session:

1. Measure Execution Time

/usr/bin/time -v python3 script.py

2. Monitor Performance Counters

perf stat python3 script.py

3. Profile Hotspots

perf record python3 script.py
perf report

Tips for Accurate Benchmarking

  • Run in isolation: Close unnecessary applications. Run on an otherwise idle system.
  • Repeat tests: Run commands multiple times and compute average.
  • Control for variability: Use representative datasets.
  • Use absolute paths: For reproducibility.
  • Take notes: Record exact command lines and environments.

Conclusion

Both time and perf are indispensable for benchmarking on Linux. time gives a fast overview of execution time, while perf offers detailed insights into performance at the CPU and system level. Use them together to understand, diagnose, and optimize your applications for speed and efficiency.


Further Reading: