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 };
> }, }
> );

To be equivalent the foreach sub should use assignment instead of
postincrement (which is also faster.)


Foreach => sub {
        my %nr;
        $nr{ $_ } = 1 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 };
 },


Also in my test using a hash slice is faster still.

Foreach => sub {
        my %nr;
        $_ = 1 foreach
                 @nr{ 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 } };
 },




John
-- 
use Perl;
program
fulfillment

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