Skip to content

Instantly share code, notes, and snippets.

@exodist
Created November 23, 2015 21:49
Show Gist options
  • Save exodist/ad0a971ea7d3e78f8269 to your computer and use it in GitHub Desktop.
Save exodist/ad0a971ea7d3e78f8269 to your computer and use it in GitHub Desktop.
perofrmance
use strict;
use warnings;
use Time::HiRes qw/time/;
use Scalar::Util qw/blessed/;
use List::Util qw/max/;
my $runs = 1_000_000;
my $thing = bless {}, 'Foo::Bar';
my %results;
my $ok = 1;
my ($start, $stop);
$start = time();
$ok &&= (blessed($thing) eq 'Foo::Bar') for 1 .. $runs;
$stop = time();
$results{blessed} = $stop - $start;
$start = time();
$ok &&= (ref($thing) eq 'Foo::Bar') for 1 .. $runs;
$stop = time();
$results{ref} = $stop - $start;
$start = time();
$ok &&= ($thing =~ m/^Foo::Bar=/) for 1 .. $runs;
$stop = time();
$results{regex} = $stop - $start;
$start = time();
$ok &&= (index($thing, 'Foo::Bar=') == 0) for 1 .. $runs;
$stop = time();
$results{index} = $stop - $start;
my $length = max(map {length($_)} keys %results);
for my $k (sort { $results{$a} <=> $results{$b} } keys %results) {
printf("%${length}s: %f\n", $k, $results{$k});
};
__END__
ref: 0.090144
index: 0.173879
blessed: 0.221761
regex: 0.303996
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment