Playing around with Method::Signatures::Simple
Method::Signatures::Simple: Nice, very simple, and almost as fast as pure perl, is this module ready for use in modules you depend on?
package Test;
use Method::Signatures::Simple;
use Benchmark qw/:all/ ;
sub new {
bless {}, shift;
}
# with Method::Signatures::Simple
method signatures ($first, $second) {
die unless $self;
die unless $first;
die unless $second;
}
# default perl
sub default {
my $self = shift;
my ($first, $second) = @_;
die unless $self;
die unless $first;
die unless $second;
}
# some benchmarking
my $self = __PACKAGE__->new;
my $call_signatures = sub {
$self->signatures(1,2)
};
my $call_default = sub {
$self->default(1,2)
};
cmpthese(10000000, {
'Signatures' => $call_signatures,
'Default' => $call_default
});
Results:
Rate Default Signatures
Default 1316309/s -- -8%
Signatures 1434103/s 9% --
seems like "signatures" is even faster than "default" (pure perl), which is quite surprising, any explanations for that?
In some occassions that can happen, but itshould not be reproducable. As the benchmark is running in sequence, some other load on the system during the "default" benchmark can simply affect the benchmark.
Here is the average result of 10 test runs...
Rate Signatures Default
Signatures 1941748/s -- -3%
Default 2000000/s 3% --
@Random get the same results again and again on my system, signatures between 3% and 15% faster (using the above script)
perl 5, version 12, subversion 1 (v5.12.1) built for MSWin32-x86-multi-thread
At this point I'd be tempted to look at subtle differences in how Method::Signatures::Simple builds the optree; e.g.
my $first = shift;
my $second = shift;
vs
my ( $first, $second ) = @_;
Tried with a 10s fixed benchmark time (-10 as first parameter to cmpthese), to give it a chance to generate stable results:
(perl5.10.1 x64 w/threads, stock Ubuntu10.10 version)
Seems logical that the extra overhead is in the shift(), since it has to modify an array, so by changing to use this:
instead, I get: on the same system. I'm surprised that signatures are so much slower here, perhaps there's overhead in a coderef or similar?