Ruby 基准违规来源
运行此代码:
require 'benchmark'
Benchmark.bm do |x|
x.report("1+1") {15_000_000.times {1+1}}
x.report("1+1") {15_000_000.times {1+1}}
x.report("1+1") {15_000_000.times {1+1}}
x.report("1+1") {15_000_000.times {1+1}}
x.report("1+1") {15_000_000.times {1+1}}
end
输出以下结果:
user system total real
1+1 2.188000 0.000000 2.188000 ( 2.250000)
1+1 2.250000 0.000000 2.250000 ( 2.265625)
1+1 2.234000 0.000000 2.234000 ( 2.250000)
1+1 2.203000 0.000000 2.203000 ( 2.250000)
1+1 2.266000 0.000000 2.266000 ( 2.281250)
猜测变化是系统环境的结果,但想确认情况确实如此。
Running this code:
require 'benchmark'
Benchmark.bm do |x|
x.report("1+1") {15_000_000.times {1+1}}
x.report("1+1") {15_000_000.times {1+1}}
x.report("1+1") {15_000_000.times {1+1}}
x.report("1+1") {15_000_000.times {1+1}}
x.report("1+1") {15_000_000.times {1+1}}
end
Outputs these results:
user system total real
1+1 2.188000 0.000000 2.188000 ( 2.250000)
1+1 2.250000 0.000000 2.250000 ( 2.265625)
1+1 2.234000 0.000000 2.234000 ( 2.250000)
1+1 2.203000 0.000000 2.203000 ( 2.250000)
1+1 2.266000 0.000000 2.266000 ( 2.281250)
Guessing the variation is a result of the system environment, but wanted to confirm this is the case.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
“猜测变化是系统环境的结果”,你是对的。
基准不可能始终准确。你没有一台完美的常规机器来总是同时运行某些东西。如果两个数字太接近,则将基准中的两个数字视为相同,如本例所示。
"Guessing the variation is a result of the system environment", you are right.
Benchmarks can't be precise all time. You don't have a perfect regular machine to run something always in the same time. Take two numbers from benchmark as the same if they were too near, as in this case.
我尝试使用
eval
部分展开循环,虽然它使速度更快,但它使执行时间不太一致!(
为了比较的目的,您在我的计算机上的基准给出了
最后一行的实时变化,但不是用户或总计)
I tried using
eval
to partially unroll the loop, and although it made it faster, it made the execution time less consistent!gave me
For the purpose of comparison, your benchmark on my computer gave
(real time varied in the last line, but not user or total)