简体   繁体   English

bash计时器(以毫秒为单位)

[英]bash timer in milliseconds

I am trying to implement a timer for 5mins(300secs) in millisecons, but it is taking more than 5 minutes for sleep 0.001. 我正在尝试以毫秒为单位实现5分钟(300秒)的计时器,但是睡眠0.001需要5分钟以上。

If I change if [ $t -ge 300000 ] to if [ $t -ge 30000 ] , it is completing within 3 minutes. 如果将if [ $t -ge 300000 ]更改为if [ $t -ge 30000 ] ,它将在3分钟内完成。

df1, df2 holds the value of disk usage value at time t1 and t2 respectively. df1,df2分别保存时间t1和t2处的磁盘使用量值。

t=0
x=0
df1
    while [ $x -lt 10000 ]

        sleep 0.001
        df2
        x=(( df1-df2 ))
        if [ $t -ge 300000 ]
        then
        t=0
        df1            
        else
        (( t++ ))
        fi
    done
dosomething

I want test $x for every 0.001secs. 我想每隔0.001秒测试$x df1 and df2 should hold new values for every 300 secs. df1df2每300秒应保存一个新值。 how can I achieve this? 我该如何实现?

date %N, will get time in nano seconds, maybe this help 日期%N,将获得以纳秒为单位的时间,也许这有帮助

start_at=$(date +%s,%N)
_s1=$(echo $start_at | cut -d',' -f1)   # sec
_s2=$(echo $start_at | cut -d',' -f2)   # nano sec
# do somethong here
end_at=$(date +%s,%N)
_e1=$(echo $end_at | cut -d',' -f1)
_e2=$(echo $end_at | cut -d',' -f2)
time_cost=$(bc <<< "scale=3; $_e1 - $_s1 + ($_e2 -$_s2)/1000000000")

I'm pretty sure that a single sleep like yours does not take several minutes. 我很确定,像您这样的一次睡眠不会花费几分钟。 While it can and, in general, will take longer than the milisecond which you requested, the real sleep time should still be well below one second, or, if the system is heavily loaded, at most a couple of seconds. 虽然它通常会比您要求的毫秒长,但实际的睡眠时间仍应远远低于一秒,或者,如果系统负载很重,则最多只有几秒钟。

One flaw in your application is that you don't take into account how long one iteration of your loop takes. 应用程序中的一个缺陷是您没有考虑循环的一次迭代需要花费多长时间。 You can't know this in advance; 您无法事先知道; you only know that it will be longer than a milisecond, but not how much longer. 您只知道会比一毫秒更长,但不会更长。 Hence, you should end the loop after a certain time has passed - ie storing the start time somewhere and comparing the current time to the start time. 因此,您应该在经过一定时间后结束循环-即将开始时间存储在某个位置,并将当前时间与开始时间进行比较。

A second problem is that you don't set anywhere the variables df1 and df2, but maybe you left out this detail because you didn't consider it relevant for your question. 第二个问题是您没有在任何地方设置变量df1和df2,但是也许您忽略了这个细节,因为您认为它与您的问题无关。

If you are looking for a countdown timer which displays milli-sec, you are looking for this. 如果您正在寻找一个显示毫秒的倒数计时器,那么您正在寻找。

This shows timer to hundredth of a second. 这将计时器显示为百分之一秒。

Change the logic as required. 根据需要更改逻辑。

    timer() {
    time="`date '+%H:%M:%S' -d "$1"`"
    if [ $# -gt 0 ] && [ "$1" = "$time" ]; then
        echo 
        IFS=:
        set -- $*
        secs=$(( ${1#0} * 3600 + ${2#0} * 60 + ${3#0} ))
        while [ $secs -ge 0 ]; do
                for msec in {99..0}; do
                printf "\rCountdown Timer: %02d:%02d:%02d:%02d" $((secs/3600)) $(( (secs/60)%60)) $((secs%60))  $((msec))
                sleep .01
                done
                secs=$(( $secs - 1 ))
        done
        echo
    fi
    echo 
    }

    Usage: # timer HH:MM:SS
    Where: HH  Time in Hours
           MM  Time in Mins
           SS  Time in Secs
    Example: 
           # timer 00:10:00 
           Sets a countdown timer for 10 mins while displaying hundredth of a second.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM