From: Benoit Daloze Date: 2012-06-19T00:54:20+09:00 Subject: [ruby-core:45701] Re: A Proposal to Improve the Method String.concat Hello, On 18 June 2012 15:16, Martin Vahi wrote: > > I'm sorry, if I'm double-posting or even triple-posting. > It seems that one has to perform mail based registration > before the posts from the http://www.ruby-forum.com/ reach the list. > > What regards to performance optimizations, then > I have Ruby and PHP demo scripts at > > https://github.com/martinvahi/mmmv_notes/tree/master/mmmv_notes/phenomenon_scrutinization/string_concatenation > > that demonstrates at least 20% performance difference > between plain string concatenation algorithm and > the versions that can be downloaded from there. I believe this is not worth it. It seems you are not aware of String#<<, and that Ruby has mutable strings. Which means there is no need for optimized repeated String#+, one can just #dup the original (if needed) and use #<<(aliased to #concat) afterwards. In numbers: n = 100000 String#concat: 0.11s user 0.01s system 97% cpu 0.115 total watershed concatenation: 0.18s user 0.03s system 97% cpu 0.215 total pseudo-watershed concatenation: 4.00s user 2.98s system 98% cpu 7.083 total plain loop concatenation: 7.33s user 7.54s system 98% cpu 15.081 total n = 1000000, String#concat: 0.88s user 0.01s system 99% cpu 0.896 total watershed concatenation: 2.04s user 0.43s system 99% cpu 2.471 total But maybe I missed something.