Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Source of Ruby benchmark irregularites

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.

like image 882
blunders Avatar asked Apr 17 '26 01:04

blunders


1 Answers

"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.

like image 168
Guilherme Bernal Avatar answered Apr 19 '26 04:04

Guilherme Bernal