I'm quite surprised by your results. It shows the reduce version to be the slowests, and it suggests that using a map with a block is faster than a for statement modifier. The former needs to enter/leave a block for each iteration, and the latter doesn't. A benchmark on my box shows different results:
#!/usr/bin/perl use strict; use warnings; use Benchmark qw /cmpthese timethese/; use List::Util qw /reduce/; our @list = 1 .. 50; our ($map_b, $map_e, $for_m, $for_b, $red, $bc, $eval); print "Perl version: $].\n"; system "cat /proc/version"; cmpthese -10 => { map_block => '$::map_b = 1; map {$::map_b *= $_} @::list', map_expr => '$::map_e = 1; map $::map_e *= $_, @::list', for_mod => '$::for_m = 1; $::for_m *= $_ for @::list', for_block => '$::for_b = 1; foreach (@::list) {$::for_b *= $_}', reduce => '$::red = reduce {$a * $b} @::list', bc => q !local $" = "*"; $::bc = `echo '@::list' | bc`!, eval => '$::eval = eval join "*" => @::list', }; print "Map block: $map_b\n"; print "Map expres: $map_e\n"; print "For modifier: $for_m\n"; print "For block: $for_b\n"; print "Reduce: $red\n"; print "bc: $bc\n"; print "Eval: $eval\n"; __END__ Perl version: 5.008003. Linux version 2.4.18-3 (bhcompile@daffy.perf.redhat.com) (gcc version +2.96 20000731 (Red Hat Linux 7.3 2.96-110)) #1 Thu Apr 18 07:37:53 ED +T 2002 Rate bc eval map_block for_block map_expr for_m +od reduce bc 2779/s -- -55% -88% -94% -95% -9 +5% -95% eval 6199/s 123% -- -73% -87% -88% -8 +8% -89% map_block 22904/s 724% 269% -- -53% -55% -5 +7% -60% for_block 49041/s 1664% 691% 114% -- -3% - +9% -13% map_expr 50669/s 1723% 717% 121% 3% -- - +6% -11% for_mod 53638/s 1830% 765% 134% 9% 6% +-- -5% reduce 56651/s 1938% 814% 147% 16% 12% +6% -- Map block: 3.0414093201713378e+64 Map expres: 3.0414093201713378e+64 For modifier: 3.0414093201713378e+64 For block: 3.0414093201713378e+64 Reduce: 3.0414093201713378e+64 bc: 30414093201713378043612608166064768844377641568960512000 +000000000 Eval: 3.0414093201713378e+64

Abigail


In reply to Re: Product of a list of numbers? by Abigail-II
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.