evanphx / benchmark-ips

Provides iteration per second benchmarking for Ruby
MIT License
1.72k stars 97 forks source link

Calculation is stuck #87

Closed prettyGoo closed 5 years ago

prettyGoo commented 6 years ago

When I try to benchmark some code, my program seems to be stuck at calculation phase after successful warm-up

I tried next piece of code (with and without manually set time and warmup):

require "benchmark/ips"

a = (1..10).to_a
b = (1..10).to_a

Benchmark.ips do |x|
  x.config(:time => 2, :warmup => 2)
  x.report("+ code performance") { a + b }
  x.report("concat code performance") {  a.concat(b) }
  x.compare!
end

ruby 2.5.1p57 (2018-03-29 revision 63029) [x86_64-darwin17] and benchmark 2.7.2 were used

evanphx commented 6 years ago

The reason is your code is getting slower and slower with each iteration because you’re growing a on every interaction. The work in the block needs to do the same work each time and you’re forcing it to allocate and copy huge amounts of memory on each iteration.

On Tue, Aug 14, 2018 at 1:46 AM Nikita Kirilov notifications@github.com wrote:

When I try to benchmark some code, my program seems to be stuck at calculation phase after successful warm-up

I tried next piece of code (with and without manually set time and warmup):

require "benchmark/ips"

a = (1..10).to_a b = (1..10).to_a Benchmark.ips do |x| x.config(:time => 2, :warmup => 2) x.report("+ code performance") { a + b } x.report("concat code performance") { a.concat(b) } x.compare!end

ruby 2.5.1p57 (2018-03-29 revision 63029) [x86_64-darwin17] and benchmark 2.7.2 were used

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/evanphx/benchmark-ips/issues/87, or mute the thread https://github.com/notifications/unsubscribe-auth/AAAAB65vicUOntcoMFqGGDAt9rA0QNEmks5uQo6AgaJpZM4V8B3d .