Command to get time in milliseconds

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

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

相关标签:
12条回答
  • 2020-12-02 04:22

    I just wanted to add to Alper's answer what I had to do to get this stuff working:

    On Mac, you'll need brew install coreutils, so we can use gdate. Otherwise on Linux, it's just date. And this function will help you time commands without having to create temporary files or anything:

    function timeit() {
        start=`gdate +%s%N`
        bash -c $1
        end=`gdate +%s%N`
        runtime=$(((end-start)/1000000000.0))
        echo " seconds"
    }
    

    And you can use it with a string:

    timeit 'tsc --noEmit'
    
    0 讨论(0)
  • 2020-12-02 04:25

    A Python script like this:

    import time
    cur_time = int(time.time()*1000)
    
    0 讨论(0)
  • 2020-12-02 04:27

    The other answers are probably sufficient in most cases but I thought I'd add my two cents as I ran into a problem on a BusyBox system.

    The system in question did not support the %N format option and doesn't have no Python or Perl interpreter.

    After much head scratching, we (thanks Dave!) came up with this:

    adjtimex | awk '/(time.tv_sec|time.tv_usec):/ { printf("%06d", $2) }'
    

    It extracts the seconds and microseconds from the output of adjtimex (normally used to set options for the system clock) and prints them without new lines (so they get glued together). Note that the microseconds field has to be pre-padded with zeros, but this doesn't affect the seconds field which is longer than six digits anyway. From this it should be trivial to convert microseconds to milliseconds.

    If you need a trailing new line (maybe because it looks better) then try

    adjtimex | awk '/(time.tv_sec|time.tv_usec):/ { printf("%06d", $2) }' && printf "\n"
    

    Also note that this requires adjtimex and awk to be available. If not then with BusyBox you can point to them locally with:

    ln -s /bin/busybox ./adjtimex
    ln -s /bin/busybox ./awk
    

    And then call the above as

    ./adjtimex | ./awk '/(time.tv_sec|time.tv_usec):/ { printf("%06d", $2) }'
    

    Or of course you could put them in your PATH

    EDIT:

    The above worked on my BusyBox device. On Ubuntu I tried the same thing and realised that adjtimex has different versions. On Ubuntu this worked to output the time in seconds with decimal places to microseconds (including a trailing new line)

    sudo apt-get install adjtimex
    adjtimex -p | awk '/raw time:/ { print $6 }'
    

    I wouldn't do this on Ubuntu though. I would use date +%s%N

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

    When you use GNU AWK since version 4.1, you can load the time library and do:

    $ awk '@load "time"; BEGIN{printf "%.6f", gettimeofday()}'
    

    This will print the current time in seconds since 1970-01-01T00:00:00 in sub second accuracy.

    the_time = gettimeofday() Return the time in seconds that has elapsed since 1970-01-01 UTC as a floating-point value. If the time is unavailable on this platform, return -1 and set ERRNO. The returned time should have sub-second precision, but the actual precision may vary based on the platform. If the standard C gettimeofday() system call is available on this platform, then it simply returns the value. Otherwise, if on MS-Windows, it tries to use GetSystemTimeAsFileTime().

    source: GNU awk manual

    On Linux systems, the standard C function getimeofday() returns the time in microsecond accuracy.

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

    To show date with time and time-zone

    date +"%d-%m-%Y %T.%N %Z"

    Output : 22-04-2020 18:01:35.970289239 IST

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

    On OS X, where date does not support the %N flag, I recommend installing coreutils using Homebrew. This will give you access to a command called gdate that will behave as date does on Linux systems.

    brew install coreutils
    

    For a more "native" experience, you can always add this to your .bash_aliases:

    alias date='gdate'
    

    Then execute

    $ date +%s%N
    
    0 讨论(0)
提交回复
热议问题