Skip to content

Instantly share code, notes, and snippets.

@sshaw
Created April 10, 2012 22:47
Show Gist options
  • Save sshaw/2355239 to your computer and use it in GitHub Desktop.
Save sshaw/2355239 to your computer and use it in GitHub Desktop.
Locale::Country::Multilingual Benchmark
# Oops, here's the right benchmark snippet.
use Locale::Country::Multilingual;
use List::MoreUtils qw(zip pairwise);
use Benchmark 'timethese';
use Unicode::Collate;
my $lcm = Locale::Country::Multilingual->new;
my $c = Unicode::Collate->new;
my ($lang, $format) = qw(en_US LOCALE_CODE_ALPHA_2);
# country2code is sooo slow, 100 should be fine
timethese(100, {
'zip+pairwise' => sub {
my @codes = $lcm->all_country_codes($format);
my @names = $lcm->all_country_names($lang);
my %countries = zip @names, @codes;
my @sorted_names = $c->sort(keys %countries);
my @sorted_codes = @countries{@sorted_names};
my @options = pairwise {
my $option = [ $a, $b ];
push @$option, selected => "selected" if $b eq 'BR';
$option;
} @sorted_names, @sorted_codes;
},
for => sub {
my @names = $lcm->all_country_names($lang);
my %options;
for (@names) {
$options{$_} = $lcm->country2code($_, $format, $lang);
}
}});
@sshaw
Copy link
Author

sshaw commented Apr 10, 2012

Benchmark: timing 100 iterations of for, zip+pairwise...
   for: 55 wallclock secs (54.98 usr +  0.63 sys = 55.61 CPU) @  1.80/s (n=100)
zip+pairwise:  4 wallclock secs ( 4.17 usr +  0.00 sys =  4.17 CPU) @ 23.98/s (n=100)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment