bash&shell Series of articles :http://www.cnblogs.com/f-ck-need-u/p/7048359.html
<http://www.cnblogs.com/f-ck-need-u/p/7048359.html#blogshell>


Sometimes you need to calculate the execution time of the command , have access to time command , although time Commands can be accurate to milliseconds , but time Command cannot calculate the execution time of a pile of commands . Can be used directly date Command calculates the time difference before and after command execution , But direct use date Command calculation time difference can only be accurate to second level . therefore , To calculate the length of time in milliseconds or microseconds , Need to date The result of the command is converted .

This paper only gives the calculation method of millisecond time difference , To calculate microsecond time differences , Just modify the script .

The script is as follows :
#!/bin/bash ########################################################### #
description: get msec level time delay # # author : Golden Dragon # # blog :
http://www.cnblogs.com/f-ck-need-u/ #
############################################################ filename:
msec_diff.sh function timediff() { # time format:date +"%s.%N", such as
1502758855.907197692 start_time=$1 end_time=$2 start_s=${start_time%.*}
start_nanos=${start_time#*.} end_s=${end_time%.*} end_nanos=${end_time#*.} #
end_nanos> start_nanos? # Another way, the time part may start with 0, which
means # it will be regarded as oct format, use "10#" to ensure # calculateing
with decimal if [ "$end_nanos" -lt "$start_nanos" ];then end_s=$(( 10#$end_s - 1
)) end_nanos=$(( 10#$end_nanos + 10**9 )) fi # get timediff time=$(( 10#$end_s
-10#$start_s )).`printf "%03d\n" $(( (10#$end_nanos - 10#$start_nanos)/10**6 ))`
echo $time } #start=$(date +"%s.%N") # Now exec some command #end=$(date
+"%s.%N")
# here give the values start=1502758855.907197692 end=1502758865.066894173
timediff $start $end
  Execute the script :
[[email protected] ~]# bash microsecond_diff.sh 9.159
The result is accurate to millisecond .

Script description :

(1). To calculate the millisecond time difference , So use date +"%s.%N" format . among "%s" Is the calculation from 1970-01-01
00:00:00 The total number of seconds passed to the current time point , So calculate two "%s" The time difference of two time points is calculated by the difference of ."%N" It's the nanosecond part of every time point , because date The command cannot directly get the time accurate to milliseconds , So it can only be calculated and converted by nanoseconds , So two time points "%N" You can calculate the time difference of the cashier in seconds .

But it should be noted that , When calculating the nanosecond time difference, consider whether to set the 1 Seconds to 10^9 nanosecond , To ensure that the nanosecond subtraction must be positive .


(2)."%N" If the length is less than 9, Will be 0 Complement . for example 999 nanosecond , Complete as 000000999. But in math , with 0 The starting value is calculated as octal by default , So we need to force it to 10 Base calculation , Need to use "10#". Refer to for relevant contents of mathematical expressions man
bash Of Arithmetic Evaluation part .

(3). because date Command acquired %s and %N In the same string , So it needs to be separated , In the above script, the method of variable segmentation is adopted .

(4). Because nanosecond to millisecond ," (end_nanos - start_nanos)/10**6
" The result of may ignore the first 0, for example "(123456789-103456789)/10**6 =
20, express 20 millisecond , When connecting integers and decimals , Need to use 0 Complement 3 Bit milliseconds , such as "1.020", So there's an extra one on it printf.