Why don't you Benchmark it?
Updated: Added my own Benchmark result.
use strict;
use Benchmark qw/ cmpthese timethese /;
use List::Util qw/ reduce /;
my @list = 1..10;
cmpthese(
timethese(1000000, {
'map_void' => '&map_void',
'for_loop' => '&for_loop',
'list_reduce' => '&list_reduce',
}));
# printf "%d, %d, %d\n", map_void(), for_loop(), list_reduce();
sub map_void {
my $result = 1;
map {$result *= $_} @list;
return $result;
}
sub for_loop {
my $result = 1;
$result *= $_ foreach @list;
return $result;
}
sub list_reduce {
return reduce { $a * $b } @list;
}
__END__
Benchmark: timing 1000000 iterations of for_loop, list_reduce, map_voi
+d...
for_loop: 4 wallclock secs ( 5.43 usr + 0.00 sys = 5.43 CPU) @ 18
+4229.92/s
(n=1000000)
list_reduce: 9 wallclock secs ( 8.46 usr + 0.00 sys = 8.46 CPU) @ 1
+18175.37/s
(n=1000000)
map_void: 5 wallclock secs ( 4.52 usr + 0.00 sys = 4.52 CPU) @ 22
+1385.88/s
(n=1000000)
Rate list_reduce for_loop map_void
list_reduce 118175/s -- -36% -47%
for_loop 184230/s 56% -- -17%
map_void 221386/s 87% 20% --
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|