Shell script example: calculate the time difference in milliseconds and microseconds, and shell microseconds

Source: Internet
Author: User
Tags time in milliseconds

Shell script example: calculate the time difference in milliseconds and microseconds, and shell microseconds

You can use the time command to calculate the execution duration of a command. Although the time command can be precise to milliseconds, the time command cannot calculate the execution time of a bunch of commands. You can also use the date command to calculate the time difference before and after the command is executed, but the time difference can only be accurate to seconds by using the date command. Therefore, to calculate the length of time in milliseconds or microseconds, You need to perform some computation and conversion on the result of the date command.

This article only describes how to calculate the millisecond-level time difference. to calculate the microsecond-level time difference, just make a slight modification to the script.

The script is as follows:


#!/bin/bash
# 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 )).$(( (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

Run the script:

[root@xuexi ~]# bash microsecond_diff.sh9.159

The visible results are accurate to milliseconds.

Script description:

(1). to calculate the time difference in milliseconds, the date + "% s. % N" format is used. "% S" is the total number of seconds from 00:00:00 to the current time point, so the difference between the two "% s" is calculated to calculate the second time difference between the two time points. "% N" is the nanoseconds of each time point. Because the date command cannot directly get the exact time to milliseconds, it can only be calculated and converted using the nanoseconds, therefore, the "% N" of the two time points can calculate the cashier's time difference in seconds.

However, when calculating the time difference between nanoseconds, consider whether to convert one second to 10 ^ 9 nanoseconds to ensure that a positive value is obtained when the nanoseconds are subtracted.

(2) If the length of the nanosecond part of "% N" is smaller than 9, it will be filled with 0. For example, 999 nm will be filled with 000000999. However, in mathematical computation, a value starting with 0 is calculated as an octal value by default. Therefore, we need to forcibly make sure that it is calculated in decimal order. We need to use "10 #". For more information about mathematical expressions, see Arithmetic Evaluation in man bash.

(3) because the % s and % N obtained by the date command are in the same string, they must be separated. In the preceding script, the variable segmentation method is used.

 

Back to series article outline: http://www.cnblogs.com/f-ck-need-u/p/7048359.html

Reprinted please indicate the source: Success!


Alibaba Cloud Hot Products

Elastic Compute Service (ECS) Dedicated Host (DDH) ApsaraDB RDS for MySQL (RDS) ApsaraDB for PolarDB(PolarDB) AnalyticDB for PostgreSQL (ADB for PG)
AnalyticDB for MySQL(ADB for MySQL) Data Transmission Service (DTS) Server Load Balancer (SLB) Global Accelerator (GA) Cloud Enterprise Network (CEN)
Object Storage Service (OSS) Content Delivery Network (CDN) Short Message Service (SMS) Container Service for Kubernetes (ACK) Data Lake Analytics (DLA)

ApsaraDB for Redis (Redis)

ApsaraDB for MongoDB (MongoDB) NAT Gateway VPN Gateway Cloud Firewall
Anti-DDoS Web Application Firewall (WAF) Log Service DataWorks MaxCompute
Elastic MapReduce (EMR) Elasticsearch

Alibaba Cloud Free Trail

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.