Elapsed time calculation refers to the process of determining the duration that has passed between two specific points in time. Measuring elapsed time is a crucial aspect of Bash scripting that allows users to perform tasks effectively and accurately.
In this article, I’ll show you how to use various techniques to calculate elapsed time in seconds, besides, how to measure elapsed time in nanoseconds and milliseconds in Bash.
What is Elapsed Time in Bash?
Elapsed time is the amount of time that passes from the start to the end of the execution of a script or command. This time interval is used to calculate the time taken to complete a particular task or a series of tasks. It can be measured in seconds, milliseconds, nanoseconds, or other units depending on the precision required. Calculating elapsed time is crucial for a variety of purposes such as script performance monitoring, optimizing resource usage, task scheduling, and so on.
3 Ways to Measure Elapsed Time in Seconds in Bash
To measure elapsed time in seconds, Bash offers various methods. The following section covers 3 different ways to measure elapsed time in seconds in Bash such as using the date command, the built-in variable $SECONDS and the time command with TIMEFORMAT.
1. Using “date” Command
The date command is used to capture timestamps and measure the time difference. You can use the format specifier +%s
to print the current time in seconds since the Unix epoch (January 1, 1970, 00:00:00 UTC (Coordinated Universal Time)) before and after an operation and to compute the time difference.
Here’s a Bash script to calculate the elapsed time using date
command:
#!/bin/bash
#Capturing the start time
start_time=$(date +%s)
#Performing task
sleep 3
#Capturing the end time
end_time=$(date +%s)
#Calculating elapsed time
elapsed=$((end_time - start_time))
#Displaying elapsed time
echo "Elapsed time is: $elapsed seconds"
Here, start_time=$(date +%s)
and end_time=$(date +%s)
capture the start and end time respectively using the date
command and the +%s
option. Sleep 3
simulates a task causing the script to pause execution for 3 seconds. elapsed=$((end_time - start_time))
calculates the elapsed time by subtracting the start time from end time and finally the script prints the elapsed time.
The image states the output of the elapsed time which is 3 seconds.
2. Using the Built-in $SECONDS Variable
The $SECONDS
is a special built-in variable in Bash that increments by one for each second elapsed since the current shell was invoked or the variable was last reset to zero (0).
When you set the value of the SECONDS variable to 0 before the code block starts, it will reset the count value and start counting the elapsed time from the point of resetting the SECONDS variable.
To calculate the elapsed time using the $SECONDS
variable, check out the script below:
#!/bin/bash
#Reset the count of SECONDS variable
SECONDS=0
#Capturing the start time
start_time=$SECONDS
#Performing tasks
sleep 2
sleep 4
#Calculating elapsed time
elapsed_time=$SECONDS
#Displaying elapsed time
echo "Elapsed time using \$SECONDS variable: $elapsed_time seconds"
Here, SECONDS=0
resets the SECONDS variable to 0. Then, start_time=$SECONDS
captures the start time by assigning the current value of the SECONDS variable. Next, sleep 2
and sleep 4
represent tasks that take 2 and 4 seconds to complete respectively. Finally, elapsed_time=$SECONDS
captures the elapsed time after the tasks have been finished and the script prints out the elapsed time.
In the image, you can see that the script displays the elapsed time between resetting $SECONDS
and the current moment in that script which is 6 seconds.
3. Employing TIMEFORMAT with “time” Command
The time command calculates the exact execution time of a script or command in Bash. By default, this command provides three time statistics:
- Elapsed (real) time (%R).
- User CPU time (%U).
- System CPU time (%S).
The TIMEFORMAT
is a special shell variable that defines the format of the output produced by the time command. When using this variable with the time command, it allows users to customize the output format and display particular time statistics using different format specifiers such as %R, %S, %U etc.
Here’s a Bash script employing the TIMEFORMAT with the time
command to get the elapsed time:
#!/bin/bash
#Specifying the format for the output of the 'time' command
TIMEFORMAT='Elapsed time is %R seconds.'
#Measuring elapsed time for the enclosed commands
time {
echo "Linux"
sleep 2
sleep 3
}
Here, TIMEFORMAT='Elapsed time is %R seconds.'
sets the format of the output where %R
specifies the elapsed time in seconds. Then, the syntax time {...}
executes the commands inside the curly braces { } and measures the elapsed time for the commands.
From the image, you can see that the elapsed time is 5.016 seconds.
%R
displays the elapsed time with 3 decimal places (fractional digits) of precision. However, you can specify the number of fractional digits using the syntax %[number_of_digit]R
. But if the specified number is greater than 3, it’ll be replaced with 3.How to Calculate Elapsed Time in Milliseconds?
To calculate elapsed time in milliseconds, use %3N
with the date
command where %3N specifies the format specifier for milliseconds.
Navigate through the following script to determine elapsed time in milliseconds:
#!/bin/bash
#Capturing start time in milliseconds
start_time=$(date +%s%3N)
#Performing a task
sleep 3
#Capturing end time in milliseconds
end_time=$(date +%s%3N)
#Calculating elapsed time in milliseconds
milli_time=$((end_time - start_time))
#Displaying elapsed time in milliseconds
echo "Elapsed time (milliseconds): $milli_time ms"
The above script captures the start time and end time in milliseconds since the UNIX epoch using the This image shows that the elapsed time is 3029 milliseconds (ms). To determine elapsed time in nanoseconds, use the Following is a Bash script showing the elapsed time calculation in nanoseconds: The above script captures the current time in nanoseconds before and after the task (2 seconds of sleep) using the The output indicates that the elapsed time is 2008345344 nanoseconds (ns). So far, you have explored different methods for elapsed time calculation in seconds as well as determining elapsed time in nanoseconds and milliseconds. To sum up, understanding and choosing the appropriate method for measuring elapsed time is crucial for proper performance monitoring in Bash scripts. To check elapsed time in Bash, you can implement the following methods: Measuring elapsed time in Bash is useful in various schemes when you need to track different tasks’ durations. Let’s see some practical use cases for measuring elapsed time in Bash: Epoch time is a system for representing time as the number of seconds that have elapsed since the Unix epoch (January 1, 1970, 00:00:00 UTC). It is also known as Unix time or POSIX time. This epoch time can be used as a universal reference point when capturing elapsed time in seconds, milliseconds, or even nanoseconds using the date command. Yes, elapsed time measurements can be affected by external factors such as high system load, intensive CPU usage, delays in I/O operations, and concurrent processes, etc. that can cause inaccurate time measurements. Yes, you can convert the measured elapsed time in milliseconds from nanoseconds. To do so, divide the elapsed time in nanoseconds by 1,000,000. For example: Related Articles << Go Back to Process Management in Bash | Bash Process and Signal Handling | Bash Scripting Tutorialdate +%s%3N
syntax. Then after performing a task (3 seconds of sleep), milli_time=$((end_time - start_time))
calculates the elapsed time in milliseconds by subtracting the value of start_time
from end_time
.How to Calculate Elapsed Time in Nanoseconds?
date
command with %N
that specifies the format specifier for nanoseconds.#!/bin/bash
#Capturing start time in nanoseconds
start_time=$(date +%s%N)
#Performing a task
sleep 2
#Capturing end time in nanoseconds
end_time=$(date +%s%N)
#Calculating elapsed time in nanoseconds
nano_time=$((end_time - start_time))
#Displaying elapsed time in nanoseconds
echo "Elapsed time (nanoseconds): $nano_time ns"
date +%s%N
syntax. Then, nano_time=$((end_time - start_time))
measures the elapsed time in nanoseconds by subtracting the start time from the end time.Conclusion
People Also Ask
How to check elapsed time in Bash?
date
command.$SECONDS
.time
command with TIMEFORMAT
.What are some practical use cases for measuring elapsed time in Bash?
What is epoch time, and how is it related to measuring elapsed time in Bash?
Can elapsed time measurements be affected by external factors?
Can I convert the measured elapsed time in milliseconds from nanoseconds?
#!/bin/bash
#Capturing start time in nanoseconds
start_time=$(date +%s%N)
#Performing a task
sleep 3
#Capturing end time in nanoseconds
end_time=$(date +%s%N)
#Calculating elapsed time in nanoseconds
nano_time=$((end_time - start_time))
#Displaying elapsed time in nanoseconds
echo "Elapsed time (nanoseconds): $nano_time ns"
#Converting elapsed time to milliseconds
milli_time=$((nano_time / 1000000))
#Displaying elapsed time in milliseconds
echo "Elapsed time (milliseconds): $milli_time ms"
FUNDAMENTALS
A Complete Guide for Beginners