in reply to Pass an array reference or array indices to a subroutine?

First, you're not passing indexes, you're passing reference to the indexes. That's surely a bug.

Secondly, apples and oranges. If you need the indexes, why aren't you passing them using "method 1"? If you don't need the indexes, why are you passing them using "method 2"?

  • Comment on Re: Pass an array reference or array indices to a subroutine?

Replies are listed 'Best First'.
Re^2: Pass an array reference or array indices to a subroutine?
by umasuresh (Hermit) on Feb 24, 2011 at 21:01 UTC
    Ikegami,
    Thanks for your suggestion. I am dereferencing the array indices before accessing the global array. May be my question should have been which one of these is faster: dereferencing an array or a scalar.
    Uma
      May be my question should have been which one of these is faster: dereferencing an array or a scalar.

      They're about the same; almost irrelevant in any real program.

      Accessing a lexical is faster than accessing a global, though the speed difference there is also almost always irrelevant.

        They're about the same; almost irrelevant in any real program.

        I propose that they're exactly the same.

        $ perl -MO=Concise,-exec -e'$$ref' 1 <0> enter 2 <;> nextstate(main 1 -e:1) v:{ 3 <$> gvsv(*ref) s 4 <1> rv2sv vK/1 5 <@> leave[1 ref] vKP/REFC -e syntax OK $ perl -MO=Concise,-exec -e'@$ref' 1 <0> enter 2 <;> nextstate(main 1 -e:1) v:{ 3 <$> gvsv(*ref) s 4 <1> rv2av[t1] vK/1 5 <@> leave[1 ref] vKP/REFC -e syntax OK

        The normal case of rv2sv boils down to a couple of checks plus

        sv = SvRV(sv);

        The normal case of rv2av boils down to a couple of checks plus

        sv = SvRV(sv);

        So any difference in performance is not intrinsic to the operation.

      May be my question should have been which one of these is faster: dereferencing an array or a scalar.

      There's no difference.

      And that shouldn't be the question at all. There's no point of comparing things that aren't equivalent.