Ruby 方法的测量和基准测试时间
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11406410/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Measure and Benchmark Time for Ruby Methods
提问by Phani
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 ?
我如何在 Ruby 中测量方法和该方法中的各个语句所花费的时间。如果您看到以下方法,我想测量该方法所花费的总时间以及数据库访问和 redis 访问所花费的时间。我不想在每个语句之前写 Benchmark.measure。ruby 解释器是否为我们提供了这样做的任何钩子?
def foo
# code to access database
# code to access redis.
end
回答by wquist
回答by ?ukasz Ostrowski
The simplest way:
最简单的方法:
require 'benchmark'
def foo
time = Benchmark.measure {
code to test
}
puts time.real #or save it to logs
end
Sample output:
示例输出:
2.2.3 :001 > foo
5.230000 0.020000 5.250000 ( 5.274806)
Values are: cpu time, system time, total and real elapsed time.
值是:cpu 时间、系统时间、总和实际经过时间。
Source: ruby docs.
来源:ruby 文档。
回答by Joshua Pinter
Use Benchmark's Report
使用Benchmark报告
require 'benchmark' # Might be necessary.
def foo
Benchmark.bm(20) do |bm| # The 20 is the width of the first column in the output.
bm.report("Access Database:") do
# Code to access database.
end
bm.report("Access Redis:") do
# Code to access redis.
end
end
end
This will output something like the following:
这将输出如下内容:
user system total real
Access Database: 0.020000 0.000000 0.020000 ( 0.475375)
Access Redis: 0.000000 0.000000 0.000000 ( 0.000037)
<------ 20 -------> # This is where the 20 comes in. NOTE: This is not shown in output.
More information can be found here.
可以在此处找到更多信息。
回答by Guy C
Many of the answers suggest the use of Time.now. But it is worth being aware that Time.nowcan change. System clocks can drift and might get corrected by the system's administrator or via NTP. It is therefore possible for Time.now to jump forward or back and give your benchmarking inaccurate results.
许多答案都建议使用Time.now. 但值得注意的是,这Time.now可能会改变。系统时钟可能会漂移,并且可能会由系统管理员或通过 NTP 进行更正。因此,Time.now 可能会向前或向后跳跃,并给出不准确的基准测试结果。
A better solution is to use the operating system's monotonic clock, which is always moving forward. Ruby 2.1 and above give access to this via:
更好的解决方案是使用操作系统的单调时钟,它总是向前移动。Ruby 2.1 及更高版本可以通过以下方式访问它:
start = Process.clock_gettime(Process::CLOCK_MONOTONIC)
# code to time
finish = Process.clock_gettime(Process::CLOCK_MONOTONIC)
diff = finish - start # gets time is seconds as a float
You can read more details here. Also you can see popular Ruby project, Sidekiq, made the switch to monotonic clock.
回答by Houcheng
A second thought, define the measure()function with Ruby code block argument can help simplify the time measure code:
第二个想法,使用 Ruby 代码块参数定义measure()函数可以帮助简化时间度量代码:
def measure(&block)
start = Time.now
block.call
Time.now - start
end
# t1 and t2 is the executing time for the code blocks.
t1 = measure { sleep(1) }
t2 = measure do
sleep(2)
end
回答by ADude2
In the spirit of wquist's answer, but a little simpler, you could also do it like below:
本着wquist 的回答的精神,但更简单一点,你也可以像下面那样做:
start = Time.now
# code to time
Time.now - start
回答by Cody Caughlan
Look into the ruby-profpackage, it should have what you need. It will create huge call stacks with timings.
查看ruby-prof包装,它应该有你需要的东西。它将创建带有计时的巨大调用堆栈。
http://ruby-prof.rubyforge.org/
http://ruby-prof.rubyforge.org/
It might be too granular, in which case just wrapping bigger sections in Benchmark.measuremight be a good way to go.
它可能太细化了,在这种情况下,将更大的部分包裹起来Benchmark.measure可能是一个好方法。
回答by Igor Kasyanchuk
Another method how to benchmark code automatically in rails console is using this gem: https://github.com/igorkasyanchuk/execution_time
另一种如何在 rails 控制台中自动对代码进行基准测试的方法是使用这个 gem:https: //github.com/igorkasynchuk/execution_time
you will see similar info as you get from requests in logs
您将看到从日志中的请求中获得的类似信息

