Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

grep, map vs. foreach performance

by Anonymous Monk
on Sep 04, 2002 at 09:19 UTC ( [id://195017]=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hello, I have two questions: A. Is there a difference between grep or map and foreach performace? B. Is there a difference between regular for loop performance and foreach performance?

Replies are listed 'Best First'.
Re: grep, map vs. foreach performance
by bronto (Priest) on Sep 04, 2002 at 10:22 UTC

    Ah, the map or foreach question!

    I remember, when gmax was not the gmax you know, that he had a problem with a slow foreach that he used to do some string to hex conversion on a bunch of blobs. I suggested him to use a more concise map, but I was wrong: foreach nearly doubled map in speed!

    After some research in the FAQ and a contemporary suggestion from a friend of us, we came up with a pure pack solution, lightning fast!

    But those were the times when gmax was not the skilled monk you know now, and many things have changed since!

    Summing up: depending on the problem you are treating and on what you put as map expression/block or inside the foreach block, foreach could result considerably faster. But if you can find a pure pack solution, it will probably be faster than both!

    Ciao!
    --bronto

    # Another Perl edition of a song:
    # The End, by The Beatles
    END {
      $you->take($love) eq $you->made($love) ;
    }

Re: grep, map vs. foreach performance
by theorbtwo (Prior) on Sep 04, 2002 at 09:44 UTC

    As to A, the most complete short answer I can give you is "Yes". The performance is definatly different between them. Which one has the best performance depends on what you're doing with them. As far as B, there is no difference. "for" and "foreach" are exactly the same, except one will get you a four-stroke penalty in golf.


    Confession: It does an Immortal Body good.

      "for" can be used as a synonym for "foreach", but what about something like "for ($i=0; $i<$max; $i++) {}"? That's a completely different thing, isn't it?

      ~Django
      "Why don't we ever challenge the spherical earth theory?"

        Hi Django,

        No, it is not a different thing. They are truly synonyms,
        foreach($i=0; $i<$max; $i++){...};
        is perfectly legal.

        cheers

        thinker.
      I change the format of a line from space delimited to tab delimited.

        Sorry, but that won't help us really a lot in helping you... There's 100 ways of converting spaces to tabs. TIMTOWTDI, remember? Well, brother theorbtwo asked what you were doing, yet here in the monastery, that word is almost synonymous with how.

        Give us a snipplet of your code, so we can do your homework for you... ;)

        Probably it's not the /for(each)?|(ma|gre)p/ that makes you slow, but actually the code you run within. Also consider that the fastest map way of doing it will quite certainly look different from the fastest for(each) way.

        But anyhow, I'd recommend trying out yourself. Ask perl. It knows best what it's fast in. Just benchmark any of the solutions you came up with, any you'll get your question anwered. Have a look at the Benchmark module, available from CPAN, for a convenient way of Benchmarking and comparing code.

        No hard feelings, but I'd really recommend to read How (Not) To Ask A Question. It's not that your question wasn't interesting -- on the contrary, but it could be even more interesting if we knew the details. ;)

        Cheers & so long,
        Flexx

Re: grep, map vs. foreach performance
by perrin (Chancellor) on Sep 04, 2002 at 17:43 UTC
    Performance is an awful reason to choose one of these commands. They have different purposes, and should be used as they were intended. The clarity of your code is more important than the small performance differences.

    In general, foreach is the one to use unless you are doing something that fits perfectly as a map or grep. map is for performing a simple transformation on a list that will return another list. grep is for selecting elements of a list that match a certain criteria (like grep'ing a file).

Re: grep, map vs. foreach performance
by Anonymous Monk on Sep 04, 2002 at 10:56 UTC
    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.

      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*

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (4)
As of 2024-04-19 04:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found