如何在 Ruby 中获得以毫秒为单位的经过时间? [英] How do I get elapsed time in milliseconds in Ruby?

查看:98
本文介绍了如何在 Ruby 中获得以毫秒为单位的经过时间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有一个 Time 对象来自:

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 milliseconds have passed? The second object may be created that same minute, over the next minutes or even hours.

推荐答案

如前所述,您可以对 Time 对象进行操作,就好像它们是数字(或浮点)值一样.这些操作会产生可以轻松转换的第二分辨率.

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.

例如:

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.

这篇关于如何在 Ruby 中获得以毫秒为单位的经过时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆