Scott R. Godin wrote:
> Interesting .. I would have thought that map would be faster, but it
> appears that foreach is, in this instance. curious.. :)
> 
> 
> 4:52pm {193} localhost:/home/webadmin/>$ perl bench.pl
> Benchmark: running Foreach, Map for at least 5 CPU seconds...
>    Foreach:  9 wallclock secs
>     ( 5.24 usr +  0.00 sys =  5.24 CPU) @ 35906.30/s (n=188149)
>        Map: 11 wallclock secs
>     ( 5.29 usr +  0.01 sys =  5.30 CPU) @ 24095.47/s (n=127706)
> 4:54pm {194} localhost:/home/webadmin/>$ cat bench.pl
> #!/usr/bin/perl
> 
> use warnings;
> use strict;
> 
> use Benchmark qw(timethese);
> 
> timethese( -5, {
> "Foreach" => sub {
>         my %nr;
>         $nr{$_}++ foreach
>                 qw{ shipto_company shipto_email billto_company
> billto_email same_bill_ship rkh_quantity rkp_quantity rmh_quantity
> rmp_quantity rbh_quantity rbp_quantity ship_on_account
> shipping_id_number order_number };
> },
> "Map" => sub {
>         my %nr = map {$_ => 1}
>                 qw{ shipto_company shipto_email billto_company
> billto_email same_bill_ship rkh_quantity rkp_quantity rmh_quantity
> rmp_quantity rbh_quantity rbp_quantity ship_on_account
> shipping_id_number order_number };
> }, }
> );
> 

Your benchmark isn't controlled. In the first instance you are doing a
++ on what amounts to a scalar getting autovivified, in the second
instance you are assigning a pair of values to a list then autovivifying
it. It isn't necessarily the map vs. foreach that is causing your
difference. I think for your benchmark to be accurate you are going to
have to control the actions taken in the loop, otherwise they affect the
accuracy of your benchmark.

http://danconia.org

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to