Skip to content

Instantly share code, notes, and snippets.

@schwern
Last active August 29, 2015 13:57
Show Gist options
  • Save schwern/9791140 to your computer and use it in GitHub Desktop.
Save schwern/9791140 to your computer and use it in GitHub Desktop.
5.20 signatures vs Method::Signatures - compile time benchmark at realistic scales
#!/usr/bin/perl
use v5.19;
use strict;
use warnings;
# This isn't strictly necessary, I just want to use them.
use feature 'signatures';
no warnings 'experimental::signatures';
use Path::Tiny;
use Benchmark qw(cmpthese);
# Create a unique package in a unique file with your choice of
# pragmas which contains a bunch of subroutines with signatures.
# This is an attempt to simulate a large production Perl system.
sub build_pm($system, $num_subs=100) {
state $package = "Test0";
state $system2pragmas = {
"5.20" =>
["use feature 'signatures'", "no warnings 'experimental::signatures'"],
"MS" => ["use Method::Signatures"],
};
state $system2keyword = {
"5.20" => 'sub',
"MS" => 'func',
};
my $pragmas = $system2pragmas->{$system};
my $keyword = $system2keyword->{$system};
$package++;
my $file = Path::Tiny->tempfile;
my $code = <<"CODE";
package $package;
CODE
$code .= join '', map { "$_;\n" } @$pragmas;
for (1..$num_subs) {
$code .= sprintf <<'CODE', $keyword, $_;
%s test%d ($a, $b, $c, $d=42) {
return $a + $b + $c + $d;
}
CODE
}
$code .= "1;\n";
# say $code;
$file->spew($code);
return $file;
}
# Build up a bunch of class files to benchmark with. We create more than
# is necessary so the benchmark can pop as many as it needs.
my @builtin_files = map {
build_pm("5.20")
} 1..1000;
my @ms_files = map {
build_pm("MS")
} 1..1000;
# Preload Method::Signatures to avoid benchmarking the one shot
# cost of loading it. Comment this out to include that cost.
require Method::Signatures;
cmpthese(25, {
builtin => sub { require pop @builtin_files; },
ms => sub { require pop @ms_files; },
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment