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% --


In reply to Re: Re: Product of a list of numbers? by Roger
in thread Product of a list of numbers? by ruffer_rinsin

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.