The Artima Developer Community
Sponsored Link

Java Buzz Forum
Ruby append (

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
dion

Posts: 5028
Nickname: dion
Registered: Feb, 2003

Dion Almaer is the Editor-in-Chief for TheServerSide.com, and is an enterprise Java evangelist
Ruby append ( Posted: Sep 29, 2006 12:22 PM
Reply to this message Reply

This post originated from an RSS feed registered with Java Buzz by dion.
Original Post: Ruby append (
Feed Title: techno.blog(Dion)
Feed URL: http://feeds.feedburner.com/dion
Feed Description: blogging about life the universe and everything tech
Latest Java Buzz Posts
Latest Java Buzz Posts by dion
Latest Posts From techno.blog(Dion)

Advertisement

A lot of developers know the dangers of concatenation with Strings and objects. In Java we had the StringBuffer.append vs. + (and now StringBuilder) knowledge transfer.

Ruby has the same issue, and people have talked about it before.

We ran into this issue in one of our projects, and I remember Dave Thomas talk about a problem that was fixed by moving from string concatenation to putting the contents on an array.

I think this benchmark says it all:

require 'benchmark'
Benchmark.bm do |x|
        x.report do
                a = 'foo'
                100000.times { a += ' foo' }
        end
        x.report do
                a = 'foo'
                100000.times { a << ' foo' }
        end
end

Output

dion@stewart [~]$ ruby t.rb
      user     system      total        real
 13.790000  25.180000  38.970000 ( 40.102451)
  0.060000   0.000000   0.060000 (  0.064342)

So, favour << unless you really want to copy strings around.

Read: Ruby append (

Topic: PeopleOverProcess.com: Podcasting Presentation Previous Topic   Next Topic Topic: PeopleOverProcess.com: RedMonk Radio Episode 26 - All About the Microsoft Open Specification Promise

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use