To subtract 60 from the value and echo that - but of course, date arithmetic isn't that simple subtracting 60 from 201908210012 will not produce 201908202312 like you would hope. Rather than perform arithmetic evaluation. Which of course will output the literal string 201908201834 - 60 If the plan is to capture the output to the variable now, the syntax for that is now=$(date -u +"%Y%m%d%H%M") This assigns an empty string to the variable now and then runs the date command as previously. If you find this tutorial helpful, I recommend you check out the series of bash shell scripting tutorials provided by Xmodulo.Your current attempt has some simple shell script errors. If you know of any trick that can improve the accuracy of elapsed time measurement in bash, feel free to share it in the comment. This proves that any sub-milliseond-level elapsed time measurement based on an external command will be highly misleading. while : doĮlapsed=$(echo "scale=6 $end_time - $start_time" | bc)Īs you can see above, elapsed time for the same operation (i.e., do nothing) does not remain constant on microsecond-level. However, such high-resolution elapsed time measurements with date command will not be anywhere near accurate due to the variable cost of invoking the external date command in a subshell.Īs a simple illustration, let's run the following bash script to measure elapsed time of noop (i.e., do nothing) with microsecond resolution. Technically it is possible to calculate elpased time with a higher resolution such as microsecond-level or nanosecond-level using date command, since date can show current time in nanosecond resolution. Measure Elapsed Time in Microseconds or in Nanoseconds in Bash # keep three digits after floating point.Įlapsed=$(echo "scale=3 $end_time - $start_time" | bc) # elapsed time with millisecond resolution Thus, with scale=3, bc computes time difference with millisecond resolution. The bc calculator allows you to specify the precision of floating point numbers (i.e., number of digits after floating point) with scale variable. Since bash does not support floating point arithmetic, you need to rely on an external calculator tool such as bc to compute time difference with millisecond resolution. To measure elapsed time with millisecond resolution, use date with +%s.%3N option, which returns the current time in milliseconds. Measure Elapsed Time in Milliseconds in Bash This will produce output like the following. If you want a more human-readable format, you can convert $elapsed output as follows. This will display elapsed time in terms of the number of seconds. Since this method does not require running the external date command in a subshell, it is a more elegant solution. When you access SECONDS variable in a bash shell, it returns the number of seconds that have passed so far since the current shell was launched. start_time=$(date +%s)Īnother (preferred) way to measure elapsed time in seconds in bash is to use a built-in bash variable called SECONDS. Thus, with this option, you can easily calculate time difference in seconds between two clock measurements. When you call date with +%s option, it shows the current system clock in seconds since 00:00:00 UTC. Using the flexiblity of date's display format, you can easily calculate elapsed time with different resolutions. The most common way to measure elapsed time in bash is to utilize the date command, which can show the current system date and time in a configurable format. In this tutorial, let's find out how to measure elapsed time in a bash script. While you are working on a bash script, you may also want to add instrumentation in your script to calculate elapsed time for various components (e.g., a bash function, an external command, etc). It is common to to measure elapsed time as part of application performance analysis and profiling. In other words, elapsed time is a measurement of actual time taken for the event to complete. Elapsed time is the amount of wall-clock time that has passed between the beginning and the end of a particular event.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |