time command

 

๐Ÿง  Command: time


๐Ÿ” Purpose

The time command is used to measure how long a command or program takes to execute.
It reports three key types of time:

  1. real — Total elapsed (wall clock) time

  2. user — CPU time spent in user mode

  3. sys — CPU time spent in kernel mode


๐Ÿงฉ Basic Syntax

time [options] command [arguments]

Example:

time ls -l

๐Ÿ“˜ Example 1: Measuring Execution Time

Run:

time sleep 3

Output:

real 0m3.003s user 0m0.000s sys 0m0.001s

๐Ÿงฉ Explanation:

FieldMeaning
real    Actual time elapsed (3.003 seconds in this case) — includes waiting time and CPU time
user    Time the CPU spent executing user code (non-kernel mode)
sys    Time the CPU spent executing system (kernel) code on behalf of the process

๐Ÿง  So here, the command sleep 3 didn’t use CPU, but it took 3 seconds of wall time just waiting.


๐Ÿ“˜ Example 2: Comparing Commands

Let’s compare two ways to print numbers from 1 to 100000.

Using echo (slow):

time for i in $(seq 1 100000); do echo -n ""; done

Using seq (fast):

time seq 1 100000 > /dev/null

You’ll notice the real time and user/sys times are much smaller for the seq command.
This helps students understand efficiency and CPU usage differences.


๐Ÿ“˜ Example 3: Using /usr/bin/time (with more details)

There are two versions of time:

  1. A shell keyword built into Bash.

  2. The external binary /usr/bin/time, which provides more detailed statistics.

Try:

/usr/bin/time -v ls

Output:

Command being timed: "ls" User time (seconds): 0.00 System time (seconds): 0.01 Percent of CPU this job got: 67% Elapsed (wall clock) time: 0:00.02 Maximum resident set size (kbytes): 2560 Major (requiring I/O) page faults: 0 Minor (reclaiming a frame) page faults: 300 Voluntary context switches: 3 Involuntary context switches: 2 ...

๐Ÿงฉ Explanation of Key Metrics:

MetricMeaning
User/System time        CPU time in user/kernel mode
Elapsed time        Total wall-clock time
CPU %        Percentage of CPU the process used
Max resident set size        Peak memory usage (in KB)
Page faults        Number of memory page accesses from disk
Context switches        How many times the CPU switched to/from this process

๐Ÿง  This version gives deeper insight into process resource usage — great for students learning about process scheduling, memory management, and CPU utilization.


๐Ÿ“˜ Example 4: Using time in a Script

Create a simple script called factorial.sh:

#!/bin/bash n=10 fact=1 for ((i=1; i<=n; i++)) do fact=$((fact * i)) done echo "Factorial of $n is $fact"

Now run:

time ./factorial.sh

You’ll see the output:

Factorial of 10 is 3628800 real 0m0.002s user 0m0.001s sys 0m0.000s

๐Ÿงฉ This demonstrates how much CPU time even a small script consumes.


๐Ÿ“˜ Example 5: Storing Time Output

You can redirect the timing result to a file:

{ time ls -R /; } 2>timelog.txt

Now check:

cat timelog.txt

This works because time outputs to stderr by default.


๐Ÿงพ Common Options Summary (for /usr/bin/time)

OptionDescription
-v       Verbose mode – show detailed stats
-p       POSIX format (portable output)
-f <format>       Custom output format
-o <file>       Write output to a file
--append       Append output to existing file

๐Ÿ“˜ Example 6: POSIX Format

/usr/bin/time -p sleep 2

Output:

real 2.00 user 0.00 sys 0.00

๐Ÿงฉ The POSIX format is simple and consistent — easy to parse in scripts.


๐Ÿงช Simple Lab Exercise

Aim:

To measure and analyze the execution time of commands using the time command.


Procedure:

  1. Run simple commands and measure their execution times:

    time ls time sleep 5 time find / -name "*.conf"
  2. Observe the real, user, and sys times.

  3. Try with the detailed /usr/bin/time -v form.

  4. Compare how CPU-intensive and I/O-intensive commands differ.


Sample Observation Table

Command    real time    user time    sys timeRemarks
time sleep 3    3.00s    0.00s    0.00s    I/O wait
time ls -l    0.01s    0.00s    0.01s        Fast
time find /    12.00s    0.30s    0.70s    Disk intensive

๐Ÿง  Learning Outcomes

Students will:

  • Understand the difference between real, user, and system time

  • Learn to measure process execution time and performance

  • Compare CPU-bound and I/O-bound operations

  • Understand how operating systems allocate time and resources to processes


๐Ÿงพ Quick Reference

CommandPurpose
time cmd        Basic timing
/usr/bin/time -v cmd        Verbose, detailed stats
/usr/bin/time -p cmd        POSIX (portable) format
/usr/bin/time -o file cmd         Save output to file
{ time cmd; } 2> file         Redirect time output to file


๐Ÿงฉ Teaching Tip

Demonstrate the difference between CPU-bound and I/O-bound tasks:

# CPU-bound time md5sum /dev/zero &> /dev/null # I/O-bound time find / -type f > /dev/null

This helps students visualize how system time increases for I/O-heavy commands and user time increases for CPU-heavy ones.

Comments

Popular posts from this blog

Operating Systems OS Lab PCCSL407 Semester 4 KTU BTech CS 2024 Scheme - Dr Binu V P

Exploring the /proc file system

ps command