in reply to When should I use map, for?

What version of perl are you using? I believe map was optimized recently. Previously it was slower than foreach.

generally speaking, I use map if I care about the return array, foreach if I don't (void context)

Perhaps you could try tr/0/./? I believe that should be faster than s///.

Replies are listed 'Best First'.
Re^2: When should I use map, for?
by radiantmatrix (Parson) on May 19, 2005 at 21:00 UTC
    I am using 5.8.6, so the delay shouldn't be a "recent fix" thing. Also, while tr/// would work for this particular case, the live-code version is not a transliteration.

    The Eightfold Path: 'use warnings;', 'use strict;', 'use diagnostics;', perltidy, CGI or CGI::Simple, try the CPAN first, big modules and small scripts, test first.

      Understood. I've noticed similar benchmark results when I ran your test. Perhaps what I am vaguely recalling is a "narrowing of the gap", so to speak.

      I refer back to my original post about when to use map vs. foreach. In this case, where it is executing many many times, it will make a difference. However, in the bulk of my coding, I use one or the other when it makes sense (primarily in a list vs. void context) and worry about performance later. I don't believe there is a time where, all other things being equal, map will be faster than foreach. I could be completely off the mark on this, however.