Created
July 29, 2009 16:42
-
-
Save gardejo/158256 to your computer and use it in GitHub Desktop.
Which is fast to replace or split?
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env perl | |
# Which is fast to replace or split? | |
use strict; | |
use warnings; | |
use Benchmark qw(cmpthese timethese); | |
use DateTime::TimeZone; | |
use Scalar::Util; | |
my $iteration = shift || 1_000_000; | |
my $timezone = DateTime::TimeZone->new(name => 'Asia/Tokyo'); | |
cmpthese( timethese( $iteration, { | |
replace_ref => sub { | |
( my $tz_name = ref $timezone ) =~ s{ \A .+ :: }{}xms; | |
return $tz_name; | |
}, | |
replace_blessed => sub { | |
( my $tz_name = Scalar::Util::blessed $timezone ) =~ s{ \A .+ :: }{}xms; | |
return $tz_name; | |
}, | |
split_ref => sub { | |
return ( split m{::}xms, ref $timezone )[-1]; | |
}, | |
split_blessed => sub { | |
return ( split m{::}xms, Scalar::Util::blessed $timezone )[-1]; | |
}, | |
} ) ); | |
__END__ | |
=head1 NAME | |
which_is_fast_to_replace_or_split - | |
=head1 DESCRIPTION | |
replaceの方が遙かに速かった。 | |
$ which_is_fast_to_replace_or_split.pl | |
Benchmark: timing 1000000 iterations of replace_blessed, replace_ref, split_blessed, split_ref... | |
replace_blessed: 2 wallclock secs ( 1.44 usr + 0.00 sys = 1.44 CPU) @ 695410.29/s (n=1000000) | |
replace_ref: 2 wallclock secs ( 0.95 usr + 0.00 sys = 0.95 CPU) @ 1049317.94/s (n=1000000) | |
split_blessed: 4 wallclock secs ( 3.30 usr + 0.00 sys = 3.30 CPU) @ 303306.04/s (n=1000000) | |
split_ref: 3 wallclock secs ( 2.98 usr + 0.00 sys = 2.98 CPU) @ 335232.99/s (n=1000000) | |
Rate split_blessed split_ref replace_blessed replace_ref | |
split_blessed 303306/s -- -10% -56% -71% | |
split_ref 335233/s 11% -- -52% -68% | |
replace_blessed 695410/s 129% 107% -- -34% | |
replace_ref 1049318/s 246% 213% 51% -- |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment