John W. Krahn wrote:
Scott R. Godin wrote:
Interesting .. I would have thought that map would be faster, but it
appears that foreach is, in this instance. curious.. :)
[snip]
To be equivalent the foreach sub should use assignment instead of
postincrement (which is also faster.)
Interestingly, not here.
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.
This, however, is true.
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 } };
},
1:17pm {48} localhost:/home/webadmin>$ perl bench.pl
Benchmark: running Foreach, Foreach2, HashSlice, Map for at least 5 CPU
seconds...
Foreach: 13 wallclock secs
( 5.32 usr + 0.02 sys = 5.34 CPU) @ 35233.90/s (n=188149)
Foreach2: 11 wallclock secs
( 5.19 usr + 0.01 sys = 5.20 CPU) @ 34134.42/s (n=177499)
HashSlice: 11 wallclock secs
( 5.18 usr + 0.00 sys = 5.18 CPU) @ 37383.20/s (n=193645)
Map: 11 wallclock secs
( 5.14 usr + 0.01 sys = 5.15 CPU) @ 23419.03/s (n=120608)
1:21pm {49} localhost:/home/webadmin>$ perl bench.pl
Benchmark: running Foreach, Foreach2, HashSlice, Map for at least 5 CPU
seconds...
Foreach: 11 wallclock secs
( 5.20 usr + 0.01 sys = 5.21 CPU) @ 35404.99/s (n=184460)
Foreach2: 11 wallclock secs
( 5.20 usr + 0.01 sys = 5.21 CPU) @ 34068.91/s (n=177499)
HashSlice: 11 wallclock secs
( 5.32 usr + 0.00 sys = 5.32 CPU) @ 37099.44/s (n=197369)
Map: 11 wallclock secs
( 5.17 usr + 0.00 sys = 5.17 CPU) @ 23328.43/s (n=120608)
Maybe I'm just seeing things, but it certainly *appears* that way I originally
wrote it, is somwhat faster than the alternate 'equivalent' you offered.
1:22pm {50} 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 };
},
"Foreach2" => 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 };
},
"HashSlice" => 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 } };
},
});
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>