On Dec 6, 2005, at 14:50, Frank Bax wrote:

At 06:06 AM 12/6/05, Jennifer Garner wrote:
I have a small script,when it run,it generate much more lines and put them
into a file.
The code for printing I writed:

 map { print RESULT $_,":",$ips{$_},"\n" }
        sort { $ips{$b} <=> $ips{$a} } keys %ips;

Certainly, I can write that code with foreach style.
I want to know which method is more effective between foreach and map?


I had basically the same question in October, with different code - check the archives (subject was "map/array performance"). I was told not to use "map" in void context ("map" has a return value and you aren't using it in this context). With my code, I found using "foreach" was slower than "map", but YMMV - do your own timings to see how it works for you. Someone was gracious enough to show me alternate code that used array operators to accomplish the same thing faster than both "map" and "foreach". Perhaps you can adapt it to your situation. Sorry, I cannot.

This is kind of a holy war. Some people have that point of view, some don't. All depends on the way you read map. The disagreement is such that recently map was optimized in void context to avoid the generation of the list.

As always, Benchmark.pm is your friend for speed comparisons. Memory is no longer misused with map in void context, so make a choice according to your personal preferences.

-- fxn


--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to