Archive for September, 2007

Ruby string joining performance

Monday, September 10th, 2007

My friend Brent (site) has been experimenting with some ruby code optimizations for joining strings. It appears you can squeeze quite a bit of more performance with a small change in code.

Here is the original code which took 13.8 seconds:

a = ''
(0..10000).each do |counter|
  a += 'a'
end

Now an alternative version that is a bit faster (0.127 seconds):

a = ''
(0..10000).each do |counter|
   a << 'a'
end

So if you want to join a lot of strings you may want to consider the alternate method. It may save you some time. :-)

You are currently browsing the furmanek.net weblog archives for September, 2007.

Social Networking

Gregory Furmanek's Facebook profile
View Gregory Furmanek's profile on LinkedIn