in reply to Re^7: "advanced" Perl functions and maintainability
in thread "advanced" Perl functions and maintainability
my $string = "this is a string" x 300 . "xyz"; cmpthese(500000, { 'index' => sub { my $res; $res = index($string, "xyz"); }, regex => sub { my $res; $res = $string =~ /xyz/; }, });
Rate regex index
regex 97087/s -- -2%
index 98619/s 2% --
most Perl hackers still pull out m//...
Ok, I agree that index is faster in some cases. However, I think there are good reasons for the behavior of most Perl hackers:
I realize that some of this reasons (particularly the last one) agree with your argument for using for and push instead of map. I just wasn't sure that your assertion regarding index was correct, as my benchmarks had shown the opposite in the past. Now I see that it depends on the situation.
Regarding the readability of map vs for, I would say that a distinct advantage of map is that it documents the purpose of the loop right at the top (when used properly). As soon as you see the map keyword you'll know that you are building a list and you'll know where it is being stored; with for, you have to wait until you see the push to see the true purpose of the loop. Which approach is better depends on the intention of the coder, and I agree that the size of the block may be a factor to consider. The problem is that the exact line between map and for is blurry, partly a matter of style and personal preference.
|
|---|