Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Re: Product of a list of numbers?

by Abigail-II (Bishop)
on Jan 23, 2004 at 10:45 UTC ( [id://323503]=note: print w/replies, xml ) Need Help??


in reply to Re: Re: Product of a list of numbers?
in thread Product of a list of numbers?

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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://323503]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (6)
As of 2024-04-25 15:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found