Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Alternative of Push operation

by mihirjha (Novice)
on Apr 28, 2008 at 10:23 UTC ( #683262=perlquestion: print w/ replies, xml ) Need Help??
mihirjha has asked for the wisdom of the Perl Monks concerning the following question:

Hi, As we know that the push operation is costly in perl, what could be the right alternative for this operation?. Regards, Mihir

Comment on Alternative of Push operation
Re: Alternative of Push operation
by andreas1234567 (Vicar) on Apr 28, 2008 at 10:44 UTC
    Last time I checked Perl was free software, so there's no cost at all.

    Seriously, first explain what you mean by the push operation is costly in perl because I don't think everyone agrees with you. Second, what application/domain of yours is it that has such requirements which makes push a particularly costly operation?

    --
    No matter how great and destructive your problems may seem now, remember, you've probably only seen the tip of them. [1]
      Second, what application/domain of yours is it that has such requirements which makes push a particularly costly operation?

      What if the array is tied to an ancient mainframe writing to paper tapes on the wrong side of a satellite connection?

Re: Alternative of Push operation
by Erez (Curate) on Apr 28, 2008 at 10:47 UTC

    As we know that the push operation is costly

    We do?

    AFAIK, push is O(1), unless I got all the efficiency stuff wrong, which is extremely efficient.

    You *could* implement your own Linked-List Data Structure in Perl and add your own push function, but that would be somewhat overkill, and wouldn't be as efficient as push which is implemented in C.

    Software speaks in tongues of man.
    Stop saying 'script'. Stop saying 'line-noise'.
    We have nothing to lose but our metaphors.

Re: Alternative of Push operation
by mscharrer (Hermit) on Apr 28, 2008 at 10:51 UTC
    From where "do we know" that the push operation is costly? Compared with what? Because it's a perl build-in it's already highly optimized C code so I don't think that any Perl code can be more efficient.
    If you look in perldoc -f push:
    [push @ARRAY, LIST] Has the same effect as for $value (LIST) { $ARRAY[++$#ARRAY] = $value; } but is more efficient.
    IMHO the best way to make your code more efficient, i.e. less-costly, is to recode you script so it's doesn't need a push operation any more.

    The most expensive thing with push might be the (re-)allocation of new memory, so if you would initialize the array with the final size at the start, e.g. $#array = 1000;, and use splice instead of push then you might save time. Please post a compare benchmark here if you code that.

      I was curious so I wrote a benchmark for this. (Please note that benchmarks must be interpreted correctly and are sometimes not very meaningful, see 588703.) The results are very interesting:
      Rate for splice splice2 push for 484262/s -- -59% -67% -76% splice 1169591/s 142% -- -21% -42% splice2 1481481/s 206% 27% -- -27% push 2020202/s 317% 73% 36% --
      As awaited the for loop is the slowest way to do it. push is the fastest of all. Using splice is quite in between of both. The interesting and surprising thing is that splice is faster without pre-allocating the array ('splice2') then with ('splice').

      The moral of this seems to be:
      Perl build-ins are already very optimized - don't try to replace them with your own Perl code.
      If you need something done which is covered by a build-in function - use it. And don't think about it. If you need more performance you are using the wrong programming language!

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://683262]
Approved by marto
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (6)
As of 2014-09-02 03:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite cookbook is:










    Results (19 votes), past polls