## 1. Overview

We define elapsed time for a block of code as how long that block takes to execute. In this tutorial, we’re going to explore how to measure elapsed time in Linux.

## 2. Using the date Command

As we know, the sleep command waits for a specified number of seconds. We’ll use it as our sample block code because it lets us define how long our code block will take to run in a somewhat precise manner.

Let’s now calculate the time elapsed using date.

Let’s create an elapsed_time.sh script:

``````start=\$(date +%s)
sleep 5
sleep 7
end=\$(date +%s)
echo "Elapsed Time: \$((\$end-\$start)) seconds"``````

We use \$() to initialize start and end variables. \$() is for command substitution. So, it runs the commands inside and captures the result in the corresponding variable.

Note that we used \$(()) for computing the result of arithmetic operations.

Let’s now run our script:

``````\$ ./elapsed_time.sh
Elapsed Time: 12 seconds``````

## 3. Using Internal Variable \$SECONDS

We can use the Bash shell’s internal variable \$SECONDS to calculate the elapsed time. When referenced, \$SECONDS outputs the number of seconds elapsed since the current shell was invoked. Let’s then make another version of our elapsed_time.sh script:

``````SECONDS=0
sleep 5
sleep 7
echo "Elapsed Time (using \\$SECONDS): \$SECONDS seconds"``````

We set the value of the SECONDS variable to zero before the code block starts. Note that using a non-integer value will set it to zero.

Once our example code block finishes, \$SECONDS contains the execution time for that code block. Note that in this case, there’s no need for arithmetic operations.

Let’s run our script again and check the result:

``````\$ ./script.sh
Elapsed Time (using \$SECONDS): 12 seconds``````

## 4. Using Bash Shell’s TIMEFORMAT

We can also use the Bash shell’s TIMEFORMAT combined with the time command to calculate the exact time spent on a block of commands with milliseconds precision. Let’s create another version of our calculation script:

``````TIMEFORMAT='It took %R seconds.'
time {
sleep 5
sleep 7
}
``````

We should wrap our commands in time{} after we define the TIMEFORMAT string. The TIMEFORMAT is a string format that will be printed after the execution of the block code inside the time{} wrapper finishes. The %R specifies to print the elapsed time in seconds with milliseconds precision.

Let’s test our script:

``````\$ ./elapsed_time.sh
It took 12.008 seconds.``````

In TIMEFORMAT, we can use a digit before R to specify the number of fractional digits to print after the decimal point. Therefore, a value of zero (%0R) causes no decimal point to be printed in the elapsed time output:

``````TIMEFORMAT='It took %0R seconds.'
time {
sleep 5
sleep 7
}``````

Let’s run our script again:

``````\$ ./elapsed_time.sh
It took 12 seconds.``````

The default value for the optional digit specifying the precision is 3. Note that if we specify any number greater than 3, it’ll be replaced with 3.

It worth noting that with the time command, we can also calculate the CPU time spent in user mode or system mode.

## 5. Elapsed Time in Nanoseconds

All these methods calculate the elapsed time in seconds or milliseconds. If we want the elapsed time in nanoseconds, we can use the date command’s %N option:

``````start=\$(date +%s%N)
sleep 5
sleep 7
end=\$(date +%s%N)
echo "Elapsed time: \$((\$end-\$start)) ns"``````

Let’s check the result:

``````\$ ./elapsed_time.sh
Elapsed time: 12023674735 ns``````

Note that if we want to have the time in milliseconds again, we should divide the nanosecond output by 1 million:

``````start=\$(date +%s%N)
sleep 5
sleep 7
end=\$(date +%s%N)
echo "Elapsed time: \$((\$((\$end-\$start))/1000000)) ms"``````

Now, let’s run this new version:

``````\$ ./elapsed_time.sh
Elapsed time: 12022 ms``````

## 6. Conclusion

In this article, we measured the elapsed time for a series of commands.

First, we calculated the elapsed time using the date command. Then, we used the \$SECONDS variable, which is the Bash shell’s internal variable that holds the elapsed time since the current shell started.

After that, we used the TIMEFORMAT built-in variable along with the %R option for calculating the elapsed time with millisecond precision.

Finally, we introduced the date command’s %N option to calculate the elapsed time down to nanoseconds.