How do I get elapsed time in milliseconds in Ruby?

前端 未结 10 2125
走了就别回头了
走了就别回头了 2020-12-13 05:27

If I have a Time object got from :

Time.now

and later I instantiate another object with that same line, how can I see how many

相关标签:
10条回答
  • 2020-12-13 05:47

    As stated already, you can operate on Time objects as if they were numeric (or floating point) values. These operations result in second resolution which can easily be converted.

    For example:

    def time_diff_milli(start, finish)
       (finish - start) * 1000.0
    end
    
    t1 = Time.now
    # arbitrary elapsed time
    t2 = Time.now
    
    msecs = time_diff_milli t1, t2
    

    You will need to decide whether to truncate that or not.

    0 讨论(0)
  • 2020-12-13 05:51

    Time.now.to_f can help you but it returns seconds.

    In general, when working with benchmarks I:

    • put in variable the current time;
    • insert the block to test;
    • put in a variable the current time, subtracting the preceding current-time value;

    It's a very simple process, so I'm not sure you were really asking this...

    0 讨论(0)
  • 2020-12-13 05:52
    DateTime.now.strftime("%Q")
    

    Example usage:

    >> DateTime.now.strftime("%Q")
    => "1541433332357"
    
    >> DateTime.now.strftime("%Q").to_i
    => 1541433332357
    
    0 讨论(0)
  • 2020-12-13 05:59

    ezpz's answer is almost perfect, but I hope I can add a little more.

    Geo asked about time in milliseconds; this sounds like an integer quantity, and I wouldn't take the detour through floating-point land. Thus my approach would be:

    irb(main):038:0> t8 = Time.now
    => Sun Nov 01 15:18:04 +0100 2009
    irb(main):039:0> t9 = Time.now
    => Sun Nov 01 15:18:18 +0100 2009
    irb(main):040:0> dif = t9 - t8
    => 13.940166
    irb(main):041:0> (1000 * dif).to_i
    => 13940
    

    Multiplying by an integer 1000 preserves the fractional number perfectly and may be a little faster too.

    If you're dealing with dates and times, you may need to use the DateTime class. This works similarly but the conversion factor is 24 * 3600 * 1000 = 86400000 .

    I've found DateTime's strptime and strftime functions invaluable in parsing and formatting date/time strings (e.g. to/from logs). What comes in handy to know is:

    • The formatting characters for these functions (%H, %M, %S, ...) are almost the same as for the C functions found on any Unix/Linux system; and

    • There are a few more: In particular, %L does milliseconds!

    0 讨论(0)
  • 2020-12-13 05:59

    Try subtracting the first Time.now from the second. Like so:

    a = Time.now
    sleep(3)
    puts Time.now - a # about 3.0
    

    This gives you a floating-point number of the seconds between the two times (and with that, the milliseconds).

    0 讨论(0)
  • 2020-12-13 06:01

    To get time in milliseconds, it's better to add .round(3), so it will be more accurate in some cases:

    puts Time.now.to_f # => 1453402722.577573
    
    (Time.now.to_f.round(3)*1000).to_i  # => 1453402722578
    
    0 讨论(0)
提交回复
热议问题