Is faster using abbreviated parameter names?

Discussion in 'Ruby' started by Iñaki Baz Castillo, Mar 2, 2009.

  1. Hi, a simple question:

    a)
    def hello(string)
    # stuff with 'string'
    end

    b)
    def hello(s)
    # stuff with 's'
    end


    Will be b) faster since the parameter name contains less letters?

    =2D-=20
    I=C3=B1aki Baz Castillo
    Iñaki Baz Castillo, Mar 2, 2009
    #1
    1. Advertising

  2. On 02.03.2009 21:48, Iñaki Baz Castillo wrote:
    > Hi, a simple question:
    >
    > a)
    > def hello(string)
    > # stuff with 'string'
    > end
    >
    > b)
    > def hello(s)
    > # stuff with 's'
    > end
    >
    >
    > Will be b) faster since the parameter name contains less letters?


    I have no idea. I guess not. But you can easily verify for yourself.
    Just write up a little program using Benchmark and you'll soon know.

    Kind regards

    robert
    Robert Klemme, Mar 2, 2009
    #2
    1. Advertising

  3. El Lunes, 2 de Marzo de 2009, Robert Klemme escribi=F3:
    > On 02.03.2009 21:48, I=F1aki Baz Castillo wrote:
    > > Hi, a simple question:
    > >
    > > a)
    > > def hello(string)
    > > # stuff with 'string'
    > > end
    > >
    > > b)
    > > def hello(s)
    > > # stuff with 's'
    > > end
    > >
    > >
    > > Will be b) faster since the parameter name contains less letters?

    >
    > I have no idea. I guess not. But you can easily verify for yourself.
    > Just write up a little program using Benchmark and you'll soon know.


    Yes, doing a benchmark the result is more or less the same (any other facto=
    r=20
    seems to be more important), but what I want to know is what should be the=
    =20
    response based on how Ruby works. Under my understanding Ruby needs to pars=
    e=20
    during runtime the variable name so a longer variable name would require mo=
    re=20
    time, am I wrong?

    Thanks a lot.


    =2D-=20
    I=F1aki Baz Castillo
    Iñaki Baz Castillo, Mar 2, 2009
    #3
  4. Iñaki Baz Castillo

    Denis Haskin Guest

    [Note: parts of this message were removed to make it a legal post.]

    And how much faster would it have to be to make it worth it to use a
    meaningless name versus a meaningful one?

    Smells like premature optimization to me...

    Cheers,

    dwh


    Robert Klemme wrote:
    > On 02.03.2009 21:48, Iñaki Baz Castillo wrote:
    >> Hi, a simple question:
    >>
    >> a)
    >> def hello(string)
    >> # stuff with 'string'
    >> end
    >>
    >> b)
    >> def hello(s)
    >> # stuff with 's'
    >> end
    >>
    >>
    >> Will be b) faster since the parameter name contains less letters?

    >
    Denis Haskin, Mar 2, 2009
    #4
  5. Iñaki Baz Castillo wrote:
    > Yes, doing a benchmark the result is more or less the same (any other
    > factor
    > seems to be more important), but what I want to know is what should be
    > the
    > response based on how Ruby works. Under my understanding Ruby needs to
    > parse
    > during runtime the variable name so a longer variable name would require
    > more
    > time, am I wrong?


    I believe you're wrong.

    Symbols are resolved into references to the symbol table at parse time,
    so when running, :s and :ssssssssssssssss are just two different
    pointers into the same symbol table. As for local variables, they are
    just offsets into the stack frame.

    So it might take a microscopically small amount of extra time for your
    program to start up, reading a few extra bytes of source code, but once
    it's running, each iteration should take the same time.
    --
    Posted via http://www.ruby-forum.com/.
    Brian Candler, Mar 2, 2009
    #5
  6. El Lunes, 2 de Marzo de 2009, Brian Candler escribi=C3=B3:
    > I=C3=B1aki Baz Castillo wrote:
    > > Yes, doing a benchmark the result is more or less the same (any other
    > > factor
    > > seems to be more important), but what I want to know is what should be
    > > the
    > > response based on how Ruby works. Under my understanding Ruby needs to
    > > parse
    > > during runtime the variable name so a longer variable name would require
    > > more
    > > time, am I wrong?

    >
    > I believe you're wrong.
    >
    > Symbols are resolved into references to the symbol table at parse time,
    > so when running, :s and :ssssssssssssssss are just two different
    > pointers into the same symbol table. As for local variables, they are
    > just offsets into the stack frame.
    >
    > So it might take a microscopically small amount of extra time for your
    > program to start up, reading a few extra bytes of source code, but once
    > it's running, each iteration should take the same time.


    Thanks, that makes sense :)

    =2D-=20
    I=C3=B1aki Baz Castillo
    Iñaki Baz Castillo, Mar 2, 2009
    #6
  7. El Lunes, 2 de Marzo de 2009, Denis Haskin escribi=C3=B3:
    > And how much faster would it have to be to make it worth it to use a
    > meaningless name versus a meaningful one?
    >
    > Smells like premature optimization to me...


    Sure, it was just curiosity :)
    I will not code in Ruby to get a unreadable code like in other languages :)



    =2D-=20
    I=C3=B1aki Baz Castillo
    Iñaki Baz Castillo, Mar 2, 2009
    #7
  8. Iñaki Baz Castillo

    Denis Haskin Guest

    [Note: parts of this message were removed to make it a legal post.]

    Good to hear.

    It's just remarkable how lots of people start at the end and work their
    way backwards... ;-)

    dwh


    Iñaki Baz Castillo wrote:
    > El Lunes, 2 de Marzo de 2009, Denis Haskin escribió:
    >
    >> And how much faster would it have to be to make it worth it to use a
    >> meaningless name versus a meaningful one?
    >>
    >> Smells like premature optimization to me...
    >>

    >
    > Sure, it was just curiosity :)
    > I will not code in Ruby to get a unreadable code like in other languages :)
    >
    >
    >
    >
    Denis Haskin, Mar 3, 2009
    #8
    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. Paddy McCarthy
    Replies:
    3
    Views:
    696
    Anthony J Bybell
    Sep 24, 2004
  2. Mike H
    Replies:
    1
    Views:
    684
    Chris Smith
    Feb 27, 2004
  3. Replies:
    4
    Views:
    428
    Justin Johansson
    Jun 25, 2007
  4. Jörg W Mittag
    Replies:
    1
    Views:
    85
    Rick DeNatale
    Jun 7, 2010
  5. Roedy Green

    abbreviated generic syntax

    Roedy Green, Feb 18, 2013, in forum: Java
    Replies:
    14
    Views:
    267
Loading...

Share This Page