From: Kornelius Kalnbach Date: 2010-03-05T06:08:51+09:00 Subject: [ruby-core:28490] Re: [Feature #905] Add String.new(fixnum) to preallocate large buffer On 04.03.10 21:08, caleb clausen wrote: > If String#<< is really O(1), there would seem to be little reason to > change anything. I still want to investigate this myself when I get a > chance. O(n), where n is the size of the appended string. But I think it's always worth to look into speedups even if we can't expect to change the complexity class. The O-factor may not interesting to theorists, but it matters greatly to programmers. JRuby, for example, concats strings almost twice as fast in this benchmark: require 'benchmark' N = 10_000_000 Benchmark.bm 20 do |results| results.report 'loop' do N.times { } end results.report "'' <<" do s = '' N.times { s << '.' << 'word' } end end ruby19 string_buffer.rb user system total real loop 1.240000 0.010000 1.250000 ( 1.255154) '' << 5.820000 0.060000 5.880000 ( 5.889959) jruby string_buffer.rb user system total real loop 0.584000 0.000000 0.584000 ( 0.488000) '' << 2.900000 0.000000 2.900000 ( 2.900000) So, there is room for optimization somewhere. [murphy]