Skip to content

Instantly share code, notes, and snippets.

@karupanerura
Created October 21, 2016 09:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save karupanerura/b83a46fc27afb454a1cb52bae45d7eea to your computer and use it in GitHub Desktop.
Save karupanerura/b83a46fc27afb454a1cb52bae45d7eea to your computer and use it in GitHub Desktop.
use strict;
use warnings;
use Benchmark qw/cmpthese timethese/;
my @class = qw/Foo Foo::Bar Foo::Bar::Baz/;
sub substr_c {
my $class = shift;
substr $class, 1 + rindex $class, ':';
}
sub split_c {
my $class = shift;
(split /::/, $class)[-1];
}
sub regexp_c {
my $class = shift;
$class =~ /:([^:]+)$/;
$1 || $class;
}
cmpthese timethese 1000000 => {
substr => sub { substr_c($_) for @class },
split => sub { split_c($_) for @class },
regexp => sub { regexp_c($_) for @class },
};
__END__
Benchmark: timing 1000000 iterations of regexp, split, substr...
regexp: 3 wallclock secs ( 2.04 usr + 0.00 sys = 2.04 CPU) @ 490196.08/s (n=1000000)
split: 2 wallclock secs ( 1.86 usr + 0.01 sys = 1.87 CPU) @ 534759.36/s (n=1000000)
substr: 1 wallclock secs ( 1.04 usr + 0.00 sys = 1.04 CPU) @ 961538.46/s (n=1000000)
Rate regexp split substr
regexp 490196/s -- -8% -49%
split 534759/s 9% -- -44%
substr 961538/s 96% 80% --
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment