Convention and performance [] versus Array.new

Discussion in 'Ruby' started by Ryan Hinton, Jan 27, 2006.

  1. Ryan Hinton

    Ryan Hinton Guest

    Is there any reason to use [] over Array.new or vice versa? Is there a
    performance benefit? Is there any convention in this area? What about
    {} versus Hash.new?

    Thanks!
    Ryan Hinton, Jan 27, 2006
    #1
    1. Advertising

  2. Ryan Hinton

    James H. Guest

    I can't directly answer your question, but something to consider is
    that Ruby has *numerous* ways to do the same thing. Just look in the
    documentation for the different ways to denote strings. I haven't
    tested it, but I tend to think that these options don't differ much in
    performance.
    James H., Jan 27, 2006
    #2
    1. Advertising

  3. Ryan Hinton

    Ryan Hinton Guest

    I completely forgot about strings. Any suggestions in that area are
    welcome as well.

    I am familiar with the Perl approach (whatever that long acronym is) of
    having lots of ways to do things. I heard about a study of different
    people's Perl code where you actually end up with different dialects.
    Beginners can write code fairly easily by using a dialect they are
    comfortable with. However, it took a Perl expert to understand code in
    someone else's dialect.

    As much as convenient, I would like to make my Ruby code readable by
    the majority of other Ruby coders (and non-Ruby coders?). So I am
    interested in following any good conventions. (Is there a good source
    of conventions and idioms for Ruby?). And if I can get a free
    performance boost by using a different idiom, I'm all for it.

    Thanks!
    Ryan Hinton, Jan 27, 2006
    #3
  4. Ryan Hinton wrote:
    > Is there any reason to use [] over Array.new or vice versa? Is there a
    > performance benefit? Is there any convention in this area? What about
    > {} versus Hash.new?
    >
    > Thanks!


    Array.new and Array.[] are pretty much totally different. Same for {}
    and Hash.new. I think the convention is to read the docs and use them
    where appropriate :)

    -Charlie
    Charles Mills, Jan 27, 2006
    #4
  5. Ryan Hinton

    Zak Kriner Guest

    I wouldn't worry about the performance in your example too much. If you
    are worried anyway you can monkey w/ the 'Benchmark' module (the docs,
    at www.ruby-doc.org/core, give examples).

    maybe something like this maybe?:

    require 'benchmark'
    puts Benchmark.measure { 100_000.times { Array.new() } }
    puts Benchmark.measure { 100_000.times { [] } }

    Regarding conventions/idioms, here are just a few links to get you
    started, I'm sure there are many more if you look around a bit.
    http://www.rubygarden.org/ruby?RubyStyleGuide
    http://www.rubygarden.org/ruby?RubyIdioms
    http://www.rubygarden.org/ruby?ExampleDesignPatternsInRuby
    http://pleac.sourceforge.net/pleac_ruby/

    Another good way to learn good ruby coding is to look at well written
    ruby code (the ruby core, rails, etc.).

    Good luck,
    zak
    Zak Kriner, Jan 27, 2006
    #5
  6. Ryan Hinton

    Ryan Hinton Guest

    Thanks for the excellent suggestions. At least on my machine (Pentium
    M laptop, 512 MB RAM) Array.new() and Hash.new() take 50% to 100%
    longer than [] and {}. Consequently, I will prefer the latter notation
    for now. I think I have read most of the pages you referenced, but I
    haven't delved into the core or Rails yet. I have kept an eye on the
    Ruby quiz, though, from which I've gleaned some good tidbits. Thanks
    again!
    Ryan Hinton, Jan 28, 2006
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Matthew Louden
    Replies:
    1
    Views:
    6,913
    Scott M.
    Oct 11, 2003
  2. jm
    Replies:
    1
    Views:
    508
    alien2_51
    Dec 12, 2003
  3. Russ

    script versus code versus ?

    Russ, Jun 10, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    2,497
  4. Christoffer Sawicki
    Replies:
    5
    Views:
    253
    Christoffer Sawicki
    Sep 2, 2006
  5. Paul Butcher
    Replies:
    12
    Views:
    710
    Gary Wright
    Nov 28, 2007
Loading...

Share This Page