Bug #4525
closedExponential performance when summing Enumerable
Description
=begin
When you sum an Enumerable (using .inject(:+) or in a more verbose fashion), 1.8.7 and 1.9.2 show exponential performance, where JRuby and Rubinius show the expected linear behavior.
class A
attr_accessor :foo
def initialize(foo)
@foo = foo
end
def +(other)
A.new(self.foo + other.foo)
end
end
(10..15).each do |factor|
start = Time.now
([A.new(2)] * 2factor * 100).inject(:+)
puts "#{2factor * 100} took #{(Time.now - start)} s."
end
Outcomes (I'm not interested in the absolute timings: note the factors between each step):
ruby 1.8.7 (2011-02-18 patchlevel 334) [x86_64-linux], MBARI 0x6770, Ruby Enterprise Edition 2011.03
102400 took 0.151034 s.
204800 took 0.345033 s.
409600 took 0.920415 s.
819200 took 2.086751 s.
1638400 took 5.727894 s.
3276800 took 18.041329 s.
ruby 1.9.2p180 (2011-02-18 revision 30909) [x86_64-linux]
102400 took 0.074562689 s.
204800 took 0.164413421 s.
409600 took 0.44690715 s.
819200 took 1.458326897 s.
1638400 took 3.215625728 s.
3276800 took 9.992734203 s.
jruby 1.5.6 (ruby 1.8.7 patchlevel 249) (2010-12-03 9cf97c3) (Java HotSpot(TM) 64-Bit Server VM 1.6.0_20) [amd64-java]
102400 took 0.705 s.
204800 took 0.206 s.
409600 took 0.222 s.
819200 took 0.37 s.
1638400 took 0.777 s.
3276800 took 1.441 s.
rubinius 1.2.4dev (1.8.7 9d6719d4 yyyy-mm-dd JI) [x86_64-unknown-linux-gnu]
102400 took 0.132361 s.
204800 took 0.138144 s.
409600 took 0.324924 s.
819200 took 0.54765 s.
1638400 took 1.0541179999999999 s.
3276800 took 2.2074 s.
=end