Skip to content

Instantly share code, notes, and snippets.

@schwern
Created August 2, 2012 19:33
Show Gist options
  • Save schwern/3239960 to your computer and use it in GitHub Desktop.
Save schwern/3239960 to your computer and use it in GitHub Desktop.
Benchmark Sub::Quote
#!/usr/bin/env perl
use strict;
use warnings;
use Sub::Quote;
use Benchmark qw(cmpthese);
my $qs = Sub::Quote::inlinify q{my $foo = 23; my $bar = 42; my $biff = 99}, '';
print "inlined sub: $qs\n";
my $qs_inline = eval "sub { $qs }";
die "Inlining failed: $@" if $@;
my $sub = sub { my $foo = 23; my $bar = 42; my $biff = 99 };
cmpthese shift || 5000000, {
# How much are we gaining over a bare sub?
sub => sub { $sub->() },
# Blocks are in the middle with a lexical declaration just to make sure they're
# not getting optimized away.
do_block => sub { my $foo = 23; do { my $bar = 42 }; my $biff = 99 },
do_block_local_at => sub { my $foo = 23; do { local @_; my $bar = 42 }; my $biff = 99 },
do_block_clear_at => sub { my $foo = 23; do { @_ = (); my $bar = 42 }; my $biff = 99 },
bare_block => sub { my $foo = 23; { my $bar = 42 }; my $biff = 99 },
bare_block_local_at => sub { my $foo = 23; { local @_; my $bar = 42 }; my $biff = 99 },
# This is closest to how inline code would run
no_block => sub { my $foo = 23; my $bar = 42; my $biff = 99 },
# A control for qs_inline to measure how much the eval is interfering
do_block_clear_at => sub { do { @_ = (); my $foo = 23; my $bar = 42; my $biff = 99 } },
qs_inline => $qs_inline,
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment