Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister

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

Replies are listed 'Best First'.
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!

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 (Priest) 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.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://683262]
Approved by marto
[tye]: that is what getlogin() certainly *used* to do. I don't believe that is what it certainly should do.
[davido]: /var/run/utmp is 664 i think.
[tye]: Note that my "man getlogin" says that it uses stdin when it should use /dev/tty (calling a glibc bug). But that does not appear to be the case when I test it. But maybe Perl's getlogin() is not using glibc's getlogin().
[oiskuu]: well, run a strace and see what the getlogin does for you.... As I said. SELinux probably has those security labels. But not regular linux.
[tye]: for example, read https://unix. questions/146138/ loginuid-should-be -allowed-to-change -or-not-mutable-or -not
[tye]: I'm not using SELinux and it certainly appears to disagree with you. shrug
[tye]: Since you brought up /proc, oiskuu, I didn't see you respond to my suggestion of 'loginuid'. Does your /proc not have such?

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (6)
As of 2017-06-23 19:46 GMT
Find Nodes?
    Voting Booth?
    How many monitors do you use while coding?

    Results (554 votes). Check out past polls.