which is faster ?

Discussion in 'Perl Misc' started by George Mpouras, Aug 4, 2011.

  1. $#array
    scalar @array or
    my $n = @array ?
     
    George Mpouras, Aug 4, 2011
    #1
    1. Advertising

  2. George Mpouras

    J. Gleixner Guest

    On 08/04/11 21:30, George Mpouras wrote:
    > $#array
    > scalar @array or
    > my $n = @array ?


    perldoc Benchmark

    Sounds like premature optimization, to me.
     
    J. Gleixner, Aug 4, 2011
    #2
    1. Advertising

  3. George Mpouras

    Dr.Ruud Guest

    On 2011-08-04 23:30, George Mpouras wrote:

    > which is faster ?
    >
    > $#array
    > scalar @array or
    > my $n = @array ?


    If magic or tie or operator-overload is not involved, there will hardly
    be any difference in speed. Just benchmark to find out.

    $#array is the highest index of @array. The other two are about the
    number of elements. $[ gives the minimum index which is normally 0.

    --
    Ruud
     
    Dr.Ruud, Aug 4, 2011
    #3
  4. George Mpouras <> wrote:
    >$#array
    >scalar @array or
    >my $n = @array ?


    IMNSHO you are asking the wrong question. Because each of these three
    code pieces has a different semantic the right question would be "Which
    code piece has the right semantic for my algorithm?"

    jue
     
    Jürgen Exner, Aug 4, 2011
    #4
  5. George Mpouras

    robin Guest

    On 04-Aug-2011 3:30 PM, George Mpouras wrote:
    > $#array
    > scalar @array or
    > my $n = @array ?

    I assume with strict the my one with work faster, I think the strict
    pragma makes it faster perl.
    -r
     
    robin, Aug 15, 2011
    #5
  6. robin <> wrote:
    >On 04-Aug-2011 3:30 PM, George Mpouras wrote:
    >> $#array
    >> scalar @array or
    >> my $n = @array ?

    >I assume with strict the my one with work faster, I think the strict
    >pragma makes it faster perl.


    Your statement can be interpreted in two ways:
    - I assume with strict the my one with work faster than the other two
    options shown
    - I assume with strict the my one with work faster than without using
    strict

    Which one is it?

    jue
     
    Jürgen Exner, Aug 15, 2011
    #6
  7. robin <> writes:
    > On 04-Aug-2011 3:30 PM, George Mpouras wrote:
    >> $#array
    >> scalar @array or
    >> my $n = @array ?

    > I assume with strict the my one with work faster, I think the strict
    > pragma makes it faster perl.


    According to the corresponding documentation, access to 'my' variables
    should be faster than acceses to 'other variables' and I expect this
    to be actually true because a variable declared with my reside in a
    scope-specific array and are accessed by array index while 'package
    globals' require a hash lookup in the symbol table hash of the package
    they belong to. But that's not at all related to 'strict' which
    reconfigures the Perl compile by modifying the variable $^H and has no
    runtime effects. (AFAIK).
     
    Rainer Weikusat, Aug 15, 2011
    #7
    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. Weng Tianxiang
    Replies:
    12
    Views:
    1,690
  2. Andreas Klemt
    Replies:
    1
    Views:
    463
    Steve C. Orr, MCSD
    Jul 23, 2003
  3. S. Justin Gengo
    Replies:
    2
    Views:
    376
    S. Justin Gengo
    Aug 20, 2003
  4. Bob
    Replies:
    1
    Views:
    2,721
  5. luca

    which one is faster?

    luca, Mar 7, 2004, in forum: Java
    Replies:
    1
    Views:
    342
    William Brogden
    Mar 8, 2004
Loading...

Share This Page