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:
-
real — Total elapsed (wall clock) time
-
user — CPU time spent in user mode
-
sys — CPU time spent in kernel mode
๐งฉ Basic Syntax
Example:
๐ Example 1: Measuring Execution Time
Run:
Output:
๐งฉ Explanation:
| Field | Meaning |
|---|---|
| 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):
Using seq (fast):
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:
-
A shell keyword built into Bash.
-
The external binary
/usr/bin/time, which provides more detailed statistics.
Try:
Output:
๐งฉ Explanation of Key Metrics:
| Metric | Meaning |
|---|---|
| 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:
Now run:
You’ll see the output:
๐งฉ 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:
Now check:
This works because time outputs to stderr by default.
๐งพ Common Options Summary (for /usr/bin/time)
| Option | Description |
|---|---|
-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
Output:
๐งฉ 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:
-
Run simple commands and measure their execution times:
-
Observe the real, user, and sys times.
-
Try with the detailed
/usr/bin/time -vform. -
Compare how CPU-intensive and I/O-intensive commands differ.
Sample Observation Table
| Command | real time | user time | sys time | Remarks |
|---|---|---|---|---|
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
Command Purpose 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
| Command | Purpose |
|---|---|
| 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:
This helps students visualize how system time increases for I/O-heavy commands and user time increases for CPU-heavy ones.
Comments
Post a Comment