BASH Programming

How to Measure Elapsed Time in Bash

Measuring the elapsed time in a bash script can provide valuable information about the performance, resource utilization, and execution time of different tasks. This information can be useful in various applications, including performance analysis, monitoring, debugging, and automated testing. This guide will discuss different ways of measuring elapsed time in a bash script.

How to Measure Elapsed Time in bash

There are several methods of measuring elapsed time in a bash script and each of these methods provides a simple and straightforward way to measure the elapsed time of a task in a bash script:

    • Through time command
    • Through date command

Through time Command

The ‘time’ command can be used to measure the elapsed time of a script or a set of commands, in addition it gives other information such as the user and system CPU time, the maximum memory usage, and the exit status of the command. Here is the syntax for using this command to get the elapsed time in shell script:

time <command>

 
To illustrate the use of this command and for educational purposes I have given an example code of bash script in which I have performed a simple task and find out the elapsed time using the above given syntax:

#!/bin/bash
time {
  result1=$((5+7))
  result2=$((457*986))
  result3=$((855+789))
  echo "Result1: $result1"
  echo "Result2: $result2"
  echo "Result3: $result3"
  sleep 2
}

 
Here is the output for the example code that demonstrate the use of time command to measure the elapsed time:


Here it just performs four tasks that are addition, subtraction, multiplication and sleeps the system for 2 seconds, the output shows that the elapsed time is almost 2 seconds.

Through date Command

Another way to find the elapsed time in bash script is by using date command, with the ‘date’ command, you can retrieve the current date and time in a specified format, and store it in a variable, here is the syntax for it:

(date [Format])

 
The Format string can include various codes for different elements of the date and time, such as %Y for the year, %m for the month, %d for the day of the month, %H for the hour, %M for the minute, and %S for the second.

To illustrate the use of this command and for educational purposes I have given an example code of bash script in which I have performed a simple task and find out the elapsed time using the above given syntax:

#!/bin/bash
start=$(date +%s)
  result1=$((5+7))
  result2=$((457*986))
  result3=$((855+789))
  sleep 2
end=$(date +%s)
elapsed=$((end-start))
  echo "Result1: $result1"
  echo "Result2: $result2"
  echo "Result3: $result3"
echo "Elapsed time: $elapsed seconds"

 
Here is the output for the example code that demonstrate the use of date command to measure the elapsed time:


Here the code just performs four tasks that are addition, subtraction, multiplication and sleeps the system for 2 seconds. To calculate the elapsed time, I have used the date command twice: one to save the start time and one to save the end time, afterwards to calculate the elapsed time it takes the difference of both times.

Conclusion

To calculate the elapsed there are two ways to do it one is by the ‘time’ and the other is by ‘date’ command. The ‘date’ command to calculate the elapsed time by storing the start time and the end time, afterwards their difference is used to calculate it. However, the ‘time’ command is a shell built-in command that can be used to measure the amount of time it takes for a command to run.

About the author

Aaliyan Javaid

I am an electrical engineer and a technical blogger. My keen interest in embedded systems has led me to write and share my knowledge about them.