in reply to Timing of Array-Size Determination Methods

sub size_index { my $val2 = $#array + 1; }

Just... don't... do... that!! Express what you *mean* or use some obfuscational language like C, (non-parrot) assembler or Java. If you want to express "if not" then type "if not". If you mean "unless", then use "unless". If you mean array size then DON'T say "index of the last element plus one".

Is scalar(@array) is slower because it requires a function call?

Yes. But don't let its being slower stop you from using it. 1.8 million per second or 3.2 million per second -- if that matters, Perl is not the right tool for the job.

Juerd # { site => 'juerd.nl', plp_site => 'plp.juerd.nl', do_not_use => 'spamtrap' }

  • Comment on Re: Timing of Array-Size Determination Methods

Replies are listed 'Best First'.
Re^2: Timing of Array-Size Determination Methods (scalar() != function)
by Aristotle (Chancellor) on Jun 02, 2003 at 23:58 UTC
    I quote perldoc -f scalar:
    Because "scalar" is unary operator
    So no it's not a function call at all, even if looks like one. (Just like split.)
    $ perl -MO=Terse -e'scalar @_' LISTOP (0x8133508) leave [1] OP (0x8133370) enter COP (0x8133400) nextstate UNOP (0x81333c0) scalar UNOP (0x8133630) rv2av [1] SVOP (0x8133540) gv GV (0x8124334) *_ -e syntax OK $ perl -MO=Terse -e'$#_+1' LISTOP (0x8133440) leave [1] OP (0x8128040) enter COP (0x8133400) nextstate BINOP (0x8133508) add [2] UNOP (0x81333c0) av2arylen UNOP (0x8133630) rv2av [1] SVOP (0x8133540) gv GV (0x8124334) *_ SVOP (0x8133370) const IV (0x8132bdc) 1 -e syntax OK
    Certainly looks like $#arr + 1 loses that argument as well. Not that it mattered in the first place..

    Makeshifts last the longest.

      All functions in perlfunc are in fact operators, I think. I believe the term "unary operator" is also used for ($)-prototyped functions, like how "list operator" is for (@)-prototyped (or prototypeless) functions.

      Juerd # { site => 'juerd.nl', plp_site => 'plp.juerd.nl', do_not_use => 'spamtrap' }

        Some further optree inspection suggests you are right; point taken. The point that, for those who care, scalar @_ compiles to 3 fewer ops than $#_ + 1 still stands though.

        Makeshifts last the longest.