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% --
In reply to Re: Re: Product of a list of numbers?
by Roger
in thread Product of a list of numbers?
by ruffer_rinsin
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |