- 论坛徽章:
- 0
|
Recently I saw a few guys writing their code with inefficient map.
I can't bear that and must point it out:
Never map in void context instead of a for loop.
When will map?
You should only use a map when you expect a list as the result returned from it.
Otherwise please use the regular for loop, which is faster and clearer.
# time perl -e 'open STDOUT,">/dev/null";map { print $_ } 1 .. 1000000'
real 0m1.204s
user 0m1.160s
sys 0m0.044s
# time perl -e 'open STDOUT,">/dev/null";print $_ for 1 .. 1000000'
real 0m0.920s
user 0m0.916s
sys 0m0.004s
So, don't think everything is cool in Perl, stop such stupid behavior please.
--Xiaolan
I don't think that using map in void context instead of a for loop will lead to any speeed increase.
In earlier perls it will lead to a slowdown.
--Paul Johnson
[ 本帖最后由 兰花仙子 于 2009-12-13 11:00 编辑 ] |
|