Command to get time in milliseconds

后端 未结 12 840
伪装坚强ぢ
伪装坚强ぢ 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
    
    0 讨论(0)
  • 2020-12-02 04:06

    Here is a somehow portable hack for Linux for getting time in milliseconds:

    #!/bin/sh
    read up rest </proc/uptime; t1="${up%.*}${up#*.}"
    sleep 3    # your command
    read up rest </proc/uptime; t2="${up%.*}${up#*.}"
    
    millisec=$(( 10*(t2-t1) ))
    echo $millisec
    

    The output is:

    3010
    

    This is a very cheap operation, which works with shell internals and procfs.

    0 讨论(0)
  • 2020-12-02 04:11

    date +%s%N returns the number of seconds + current nanoseconds.

    Therefore, echo $(($(date +%s%N)/1000000)) is what you need.

    Example:

    $ echo $(($(date +%s%N)/1000000))
    1535546718115
    

    date +%s returns the number of seconds since the epoch, if that's useful.

    0 讨论(0)
  • 2020-12-02 04:14

    Nano is 10−9 and milli 10−3. Hence, we can use the three first characters of nanoseconds to get the milliseconds:

    date +%s%3N
    

    From man date:

    %N nanoseconds (000000000..999999999)

    %s seconds since 1970-01-01 00:00:00 UTC

    Source: Server Fault's How do I get the current Unix time in milliseconds in Bash?.

    0 讨论(0)
  • 2020-12-02 04:19

    Perl can be used for this, even on exotic platforms like AIX. Example:

    #!/usr/bin/perl -w
    
    use strict;
    use Time::HiRes qw(gettimeofday);
    
    my ($t_sec, $usec) = gettimeofday ();
    my $msec= int ($usec/1000);
    
    my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) =
        localtime ($t_sec);
    
    printf "%04d-%02d-%02d %02d:%02d:%02d %03d\n",
        1900+$year, 1+$mon, $mday, $hour, $min, $sec, $msec;
    
    0 讨论(0)
  • 2020-12-02 04:20
    • date +"%T.%N" returns the current time with nanoseconds.

      06:46:41.431857000
      
    • date +"%T.%6N" returns the current time with nanoseconds rounded to the first 6 digits, which is microseconds.

      06:47:07.183172
      
    • date +"%T.%3N" returns the current time with nanoseconds rounded to the first 3 digits, which is milliseconds.

      06:47:42.773
      

    In general, every field of the date command's format can be given an optional field width.

    0 讨论(0)
提交回复
热议问题