Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
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":



  • 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.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (8)
As of 2024-04-19 14:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found