use v5.16;
use warnings;
use Benchmark 'cmpthese';
use Test::More;
package SMT::test {
use Sub::Multi::Tiny qw( $foo $bar );
sub first :M($foo, $bar) {
return $foo ** $bar;
}
sub second :M($foo) {
return $foo + 42;
}
}
package M {
use Kavorka qw(multi fun);
multi fun test ($foo, $bar) {
return $foo ** $bar;
}
multi fun test ($foo) {
return $foo + 42;
}
}
is(M::test(8,2), 64);
is(M::test(3,3), 27);
is(M::test(58), 100);
is(SMT::test(8,2), 64);
is(SMT::test(3,3), 27);
is(SMT::test(58), 100);
cmpthese -1, {
M => q{ M::test(3,3); SMT::test(0); },
SMT => q{ SMT::test(3,3); SMT::test(0); },
};
Not really an issue, but thought I'd send you these.
Benchmark source
Results on my laptop:
I had expected a bit of a slow down from your use of guards and global variables, but it actually performs at a pretty decent rate.
As you use a stringy eval to build your dispatcher, you could probably speed things up a little by avoiding the hashtable arity lookup thing at run time, and putting
if(@_==1) {...} elsif(@_==2) {...}into the evaluated string itself. The only advantage in keeping the hash table lookup would be if you wanted to be able to insert new entries into it at run-time, but right now the hash is a lexical variable, so short of some PadWalker tricks, can't be altered by runtime code anyway.