Sometimes it is necessary to calculate the execution time of the command, which can be used, although the time command can be accurate to the millisecond level, but it is not possible to calculate the execution times of a heap of commands. You can also use the date command to calculate the time difference before and after the execution of a command, but using the date command to calculate the difference can only be accurate to the second level. Therefore, to calculate the length of milliseconds or microseconds, the result of the date command needs to be computed and converted.
This paper only gives the millisecond time difference calculation method, to calculate the microsecond time difference, the script changes slightly.
The script is as follows:
#!/bin/Bash# filename:msec_diff. SHfunctionTimediff () {# Time format:date + "%s.%n", such as 1502758855.907197692start_time=$1End_time=$2start_s=${start_time%.*} Start_nanos=${start_time#*.} end_s=${end_time%.*} End_nanos=${end_time#*.} # End_nanos > Start_nanos? # Another, the time part may start with 0, which means # It'll be regarded as Oct format, use "10#" to ensure # Calculateing with decimal if["$end _nanos"-lt"$start _nanos"]; Thenend_s=$((Ten# $end _s-1)) End_nanos=$((Ten# $end _nanos +Ten**9 )) fi# Get Timediff Time=$((Ten# $end _s-Ten# $start _s)). $ (((Ten# $end _nanos-Ten# $start _nanos)/Ten**6 )) Echo$ Time}#start =$ (date + "%s.%n") # now exec some command#end=$ (date + "%s.%n"))
# here give the valuesstart=1502758855.907197692end=1502758865.066894173
Timediff $start $end
Execute the script:
[Email protected] ~]# bash Microsecond_diff.sh9.159
The visible results are accurate to the millisecond level.
Script Description:
(1). In order to calculate the millisecond time difference, use the date + "%s.%n" format. Where "%s" is the total number of seconds calculated from 1970-01-01 00:00:00 to the current point in time, so the difference of two "%s" calculates the second time difference of two points. "%N" is the nanosecond portion of each point in time, because the date command cannot directly get the exact millisecond time, so it can only be calculated and converted by nanosecond, so the "%N" of the two-point time can calculate the second-level difference between the cashier.
However, it is important to note that when calculating the nanosecond time difference, consider whether you want to convert 1 seconds to 10^9 nanoseconds to ensure that a positive value is obtained when the nanosecond is subtracted.
(2). " %N "of the nanosecond portion if the length is less than 9, will be 0. For example, 999 nm, will be filled to 000000999. However, in mathematical calculations, a value starting with 0 will be calculated as octal by default, so it is necessary to force it to be calculated as a 10-binary, using "10#". Refer to the Arithmetic evaluation section of the man bash for the relevant contents of the mathematical expression.
(3). Because the date command obtains the%s and%n within the same string, it needs to be split, in the above script is a method of variable segmentation.
Back to series article outline:http://www.cnblogs.com/f-ck-need-u/p/7048359.html
Reprint Please specify source:http://www.cnblogs.com/f-ck-need-u/p/7426987.html Note: If you think this article is not bad please click on the lower right corner of the recommendation, with your support to inspire the author more enthusiasm for writing, thank you very much!
Shell Script Example: calculate millisecond, microsecond time difference