本文介绍了测量和基准时间的Ruby方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在Ruby中,如何测量方法以及该方法中的各个语句所花费的时间.如果您看到以下方法,我想测量该方法花费的总时间以及数据库访问和Redis访问所花费的时间.我不想在每条语句之前写Benchmark.measure.红宝石解释器会为我们提供任何帮助吗?
How can i measure the time taken by a method and the individual statements in that method in Ruby. If you see the below method i want to measure the total time taken by the method and the time taken for database access and redis access. I do not want to write Benchmark.measure before every statement. Does the ruby interpreter gives us any hooks for doing this ?
def foo
# code to access database
# code to access redis.
end
推荐答案
您可以使用Time
对象. (时间文档)
You could use the Time
object. (Time Docs)
例如,
start = Time.now
# code to time
finish = Time.now
diff = finish - start
diff
将以秒为单位,作为浮点数.
diff
would be in seconds, as a floating point number.
end
已保留.
这篇关于测量和基准时间的Ruby方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!