in reply to grep, map vs. foreach performance

They do different things. grep and map build return lists. for and equivalently foreach do not. If you want a return list, then it is faster to use grep and map than to build one in Perl. If you don't need that list then it is slow and wasteful of memory to build those lists.

So yes, each is faster at what they are built for. Each is worse at doing what they weren't built for.

Replies are listed 'Best First'.
Re^2: grep, map vs. foreach performance
by Flexx (Pilgrim) on Sep 04, 2002 at 13:13 UTC

    Hmmm.. I'm inclined to doubt that this is true for all cases. In principle, I'd agree, but saying that anything expecting a return list is too simple an assumtion.

    I'm quite sure (although I'm too lazy now, to come up with some proof) that I'd find a map with a discarded return list that does something faster than an equivalent for/foreach.

    BTW, has someone thought that a smart while() might be fastest anyway?

      while is for...

      > perl -MO=Deparse -e"for(;;){}" for (;;) { (); } -e syntax OK > perl -MO=Deparse -e"while(1){}" for (;;) { (); } -e syntax OK > perl -MO=Deparse -e"for(1){}" foreach $_ (1) { (); } -e syntax OK >

      ~Particle *accelerates*

        > perl -MO=Deparse -e 'while($x++ < 10){}' while ($x++ < 10) { (); }

        Now, why is that? Shouldn't that be

        for(;$x++ < 10;){}

        then? On the contrary actually:

        > perl -MO=Deparse -e 'for(;();){}' while (()) { (); }

        So, it seems that while is for only for the obvious endless loop, and in other cases for is while (as I had expected...)

        However, I am intriqued by the fact that

        > perl -MO=Deparse -e 'for($x;$y;$z){()}' for ($x; $y; $z) { (); }

        and not

        { $x; while($y) { (); } continue { $z; } }

        (at least not on the Perl level of things).

        Cool stuff anyway! Thanks a million. I learned a lot from your reply. I flew over the optimizer and Deparse a few times (mostly when searching for other things), but using it that way looks powerful. Got any pointers to good information on it? Is there some "How to wrink the guts out of your script using -MO=Deparse/the Optimizer" somewhere?

        So long,
        Flexx

        (I like the photo on your home node, BTW).

        No, while isn't for. It is optimized to a for(;;) in the case where it is used as while(SOME_TRUE_CONSTANT). It is also optimized away in the case where it is used as while(SOME_FALSE_CONSTANT).

        $ perl -MO=Deparse -e 'while (0) {}' ; -e syntax OK $ perl -MO=Deparse -e 'while (1) {}' for (;;) { (); } -e syntax OK $ perl -MO=Deparse -e 'while (@_) {}' while (@_) { (); } -e syntax OK

        For that matter, even though for and foreach are synonymous, what's in the parens determines how perl actually parses them:

        $ perl -MO=Deparse -e 'for (;;) {}' for (;;) { (); } -e syntax OK $ perl -MO=Deparse -e 'foreach (;;) {}' for (;;) { (); } -e syntax OK $ perl -MO=Deparse -e 'for (@_) {}' foreach $_ (@_) { (); } -e syntax OK $ perl -MO=Deparse -e 'foreach (@_) {}' foreach $_ (@_) { (); } -e syntax OK
        -sauoq
        "My two cents aren't worth a dime.";