bash&shell Serial 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 totime command, althoughtime Commands can be accurate to milliseconds, buttime Command cannot calculate the execution time of a pile of commands. Can be used directlydate Command calculates the time difference before and after command execution, But direct usedate Command calculation time difference can only be accurate to second level. therefore, To calculate the length of time in milliseconds or microseconds, Need todate The result of the command is converted.

This paper only gives the calculation method of millisecond time difference, To calculate microsecond time difference, 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 usedate +"%s.%N" format. among"%s" It's computation.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, Becausedate 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 the1 Second conversion10^9 nanosecond, To ensure that the nanosecond subtraction must be positive.


(2)."%N" If the length is less than9, Will take0 Make up. for example999 nanosecond, Will be added to000000999. But in math, with0 The starting value is calculated as octal by default, So we need to force it to10 Metric computation, Need to use"10#". Refer to for relevant contents of mathematical expressionsman
bash OfArithmetic Evaluation Part.

(3). Becausedate 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 first0, for example"(123456789-103456789)/10**6 =
20, Express20 Millisecond, When connecting integers and decimals, Need to use0 Make up3 Bit millisecond, such as"1.020", So there's an extra one on itprintf.