Measure and Benchmark Time for Ruby Methods

2020-05-11 21:18发布

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

7条回答
一纸荒年 Trace。
2楼-- · 2020-05-11 22:00

In the spirit of wquist's answer, but a little simpler, you could also do it like below:

start = Time.now
# code to time
Time.now - start
查看更多
登录 后发表回答