http://www.perlmonks.org?node_id=192136


in reply to Re: Re: Optimizing existing Perl code (in practise)
in thread Optimizing existing Perl code (in practise)

Hy,

The "shift" options is good to use when you send big data to the function! The process of the command is not fast, because it need to cut the value from the array, reorder the array, and create and save to a scalar variable! "shift" is good to use for big data because you don't leave in the memory the data 2 times! You just move to the scalar! If you want speed use first the @_[0], then if you need to change the data inside @_[0], you use my ($var) = @_ ;, and if you have big data you use the "shift".

"The creativity is the expression of the liberty".

Replies are listed 'Best First'.
Re: Re: Re: Re: Optimizing existing Perl code (in practise)
by RMGir (Prior) on Aug 22, 2002 at 19:00 UTC
    The "shift" options is good to use when you send big data to the function! The process of the command is not fast, because it need to cut the value from the array, reorder the array, and create and save to a scalar variable! "shift" is good to use for big data because you don't leave in the memory the data 2 times!

    Good point!

    I'd try to avoid that by passing a reference to the large scalar to the sub instead, but if you're expecting possibly large scalar arguments by value (edit: because you need to modify them, maybe), you're right.
    --
    Mike

      "I'd try to avoid that by passing a reference to the large scalar to the sub instead"
      Better point! :)

      The reference is another good way! But is good only if you just want to past the data through the sub, because you can't read or edit it before parse the reference, and when you parse it you duplicate again the data in the memory.

      Of course that the best idea is always the simplest, why not put the variable with the "big data" in global (without 'my' or 'local'), to be read by all the program?
      K.I.S.S. -> Kip It Simple, Stupid! :-P

      "The creativity is the expression of the liberty".

        Of course that the best idea is always the simplest, why not put the variable with the "big data" in global (without 'my' or 'local'), to be read by all the program? K.I.S.S. -> Kip It Simple, Stupid! :-P

        Nope. That would actually make things more complicated, since now you have to remember that this subroutine affects that global. It tends to make things more fragile, and much harder to follow for maintainers.

        The global has no benefits whatsoever over passing data in by reference. You can modify data via a reference just as well as you can with a global...
        --
        Mike