one said:
compared the functions, in case it is useful to know...
Benchmark: timing 2000000 iterations of a, b...
a-ARRAY: 5 wallclock secs ( 0.13 usr + 0.11 sys = 0.24 CPU)
@ 8333333.33/s (n=2000000)
(warning: too few iterations for a reliable count)
b-HASH: 1 wallclock secs ( 0.20 usr + -0.02 sys = 0.18 CPU) @
11111111.11/s (n=2000000)
(warning: too few iterations for a reliable count)
Rate a b
a 8333333/s -- -25%
b 11111111/s 33% --
Hi,
which functions are you comparing ?
Using the code below I get output as follows:
Rate match_City match_city_2
match_City 96006/s -- -85%
match_city_2 632111/s 558% --
ripon
ripon
yorba linda
yorba linda
not found
Not Found
#
use strict;
use warnings;
use Benchmark qw
all) ;
my @CA_Cities = (
"adelanto", "agoura hills", "alameda", "alamo", "albany",
"alhambra",
"newman", "newport beach", "nipomo", "norco", "north auburn",
"north highlands", "norwalk", "novato", "oakdale", "oakland",
"oceanside", "oildale", "ojai", "olivehurst", "ontario", "opal cliffs",
"orange cove", "orangevale", "orcutt", "orinda", "orland", "orosi",
"oroville east", "oxnard", "pacific grove", "pacifica", "palm desert",
"palmdale", "palo alto", "palos verdes estates", "paradise", "paramount",
"parkway-south sacramento", "parlier", "pasadena", "patterson",
"petaluma", "pico rivera", "piedmont", "pinole", "pismo beach",
"placentia", "placerville", "pleasant hill", "pleasanton", "pomona",
"porterville", "portola hills", "poway", "prunedale", "quartz hill",
"ramona",
"rancho cordova", "rancho cucamonga", "rancho mirage",
"rancho san diego", "rancho santa margarita", "red bluff", "redding",
"redlands", "redondo beach",
"redwood city", "reedley", "rialto", "richmond", "ridgecrest",
"rio del mar", "ripon",
"west whittier-los nietos", "westlake village", "westminster",
"westmont",
"whittier", "wildomar", "willowbrook", "willows", "windsor", "winter
gardens","winters", "winton", "woodcrest", "woodlake", "woodland",
"yorba linda", "yreka", "yuba city", "yucaipa", "yucca valley"
);
my %h_Cities = map { $_ => 1 } @CA_Cities;
use integer;
sub match_City {
my $findme = $_[0];
my( $start, $end ) = ( 0, $#CA_Cities );
my $idx ;
$findme = lc $findme;
while ( $start < $end -1) {
$idx = ( $end + $start ) / 2 ;
if ( $CA_Cities[ $idx ] lt $findme ) {
$start = $idx;
} elsif ( $CA_Cities[ $idx ] gt $findme ) {
$end = $idx;
} else {
return $findme;
}
}
return "not found";
}
sub match_city_2{
my $findme = $_[0];
$findme = lc $findme;
if (exists ($h_Cities{$findme})){
return $findme;
} else {
return "Not Found";
}
}
cmpthese(500000, {
'match_City' =>sub{match_City( "fred" )},
'match_city_2' => sub {match_city_2( "fred" )}
});
## Test this
for ("Ripon","yorba linda","fred"){
print "\n" . match_City( $_ );
print "\n" . match_city_2( $_ );
}