Command to get time in milliseconds

后端 未结 12 964
伪装坚强ぢ
伪装坚强ぢ 2020-12-02 04:01

Is there a shell command in Linux to get the time in milliseconds?

12条回答
  •  臣服心动
    2020-12-02 04:05

    date command didnt provide milli seconds on OS X, so used an alias from python

    millis(){  python -c "import time; print(int(time.time()*1000))"; }
    

    OR

    alias millis='python -c "import time; print(int(time.time()*1000))"'
    

    EDIT: following the comment from @CharlesDuffy. Forking any child process takes extra time.

    $ time date +%s%N
    1597103627N
    date +%s%N  0.00s user 0.00s system 63% cpu 0.006 total
    

    Python is still improving it's VM start time, and it is not as fast as ahead-of-time compiled code (such as date).

    On my machine, it took about 30ms - 60ms (that is 5x-10x of 6ms taken by date)

    $ time python -c "import time; print(int(time.time()*1000))"
    1597103899460
    python -c "import time; print(int(time.time()*1000))"  0.03s user 0.01s system 83% cpu 0.053 total
    

    I figured awk is lightweight than python, so awk takes in the range of 6ms to 12ms (i.e. 1x to 2x of date):

    $ time awk '@load "time"; BEGIN{print int(1000 * gettimeofday())}'
    1597103729525
    awk '@load "time"; BEGIN{print int(1000 * gettimeofday())}'  0.00s user 0.00s system 74% cpu 0.010 total
    

提交回复
热议问题