|Perl: the Markov chain saw|
map versus forby dHarry (Abbot)
|on Aug 04, 2008 at 14:40 UTC||Need Help??|
dHarry has asked for the
wisdom of the Perl Monks concerning the following question:
I was contemplating over the 256 commandments from Damianís "Perl Best Practices" when I encountered:
Being more used to other programming languages I often use the non-Perl approach to get the job done. I would typically use a for and probably not even consider the map. So this was an eye-opener for me. I decided to do a little test to see how much the difference is between the two.
(FYI: Perl v5.8.8 built for MSWin32-x86-multi-thread running on a Dell INSPIRON 9400)
I use the example as mentioned by Damian and the Benchmark module to test:
First the comparison:
Hmmm, not really impressive this ďgainĒ of using map over for?!
Next some timing:
Am I missing something? Is the example given by Damian a poor example? Should I really favor map over for when I want to generate a new list from another list?
Beside the obvious advantages: less code, easier to understand, it is stated that map is normally considerably faster.