How do I get elapsed time in milliseconds in Ruby?

前端 未结 10 2124
走了就别回头了
走了就别回头了 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.

提交回复
热议问题