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

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
  • Comment on Re^2: Pass an array reference or array indices to a subroutine?

Replies are listed 'Best First'.
Re^3: Pass an array reference or array indices to a subroutine?
by chromatic (Archbishop) on Feb 24, 2011 at 21:38 UTC
    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.

Re^3: Pass an array reference or array indices to a subroutine?
by ikegami (Patriarch) on Feb 24, 2011 at 21:06 UTC

    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.