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

Re: question for perl book & magazine authors

by sauoq (Abbot)
on Oct 19, 2005 at 21:00 UTC ( #501426=note: print w/replies, xml ) Need Help??

in reply to question for perl book & magazine authors

I'm convinced that map & grep aren't as heavily used as they could be because people don't understand them.

I don't think I agree with the implied premise that map() and grep() should be used more heavily. I'd say they are probably used inappropriately at least as often as appropriate uses for them are missed.

For instance, in the example you gave, you'd probably be better off using a hash to determine common elements.

Update: You should also be more careful to check your benchmarks for correctness. Your "using grep" benchmark doesn't even reference @array1...

Update 2: I also just noticed you are using grep in a scalar context in the unless clause in your "using foreach" sub. That's one of those inappropriate uses of grep I mentioned above...

"My two cents aren't worth a dime.";

Replies are listed 'Best First'.
Re^2: question for perl book & magazine authors
by holli (Monsignor) on Oct 19, 2005 at 21:06 UTC
    The hash trick is a nice way if the array is small enough. for huge arrays the algorhitmical/runtime trade may be worth the price to save memory.

    holli, /regexed monk/

      Sure... if memory is so tight that you have to choose an O(n * m) time complexity over O(n + m) then I guess you gotta do what you gotta do.

      Of course, in that case (and especially if this is a routine task) you are almost certainly going to win by throwing more RAM at the problem because the larger your dataset the more (time) you are saving with the linear algorithm.

      Update: Also note that the additional memory you need will be relative to the smaller of your two arrays. (As a possible optimization depending, of course, on your data.)

      "My two cents aren't worth a dime.";

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://501426]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (2)
As of 2018-05-23 04:27 GMT
Find Nodes?
    Voting Booth?