Skip to content

Instantly share code, notes, and snippets.

@zoffixznet
Created June 5, 2017 08:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zoffixznet/30ba2fbc0244a9d19395b4c9486bb157 to your computer and use it in GitHub Desktop.
Save zoffixznet/30ba2fbc0244a9d19395b4c9486bb157 to your computer and use it in GitHub Desktop.
Bare: 0.028753594768s
Old: 9.173754254014297434905s
New: 0.163172s
NEW version is 56.22x faster
use Benchy;
sub old (\SELF, \b) {
nqp::if(
nqp::iseq_i(0, $_ := nqp::add_i(
nqp::istype(b, Numeric), nqp::istype(b, Numeric)))
|| nqp::iseq_i($_, 2),
SELF.Int.ACCEPTS(b) && SELF.Str.ACCEPTS(b),
nqp::if(
nqp::istype(b, Str),
SELF.Str.ACCEPTS(b),
SELF.Int.ACCEPTS(b)))
}
sub new (\SELF, \b) {
nqp::if(
nqp::istype(b, Numeric),
nqp::if(
nqp::istype(b, Str),
SELF.Int.ACCEPTS(b) && SELF.Str.ACCEPTS(b),
SELF.Int.ACCEPTS(b)),
nqp::if(
nqp::istype(b, Str),
SELF.Str.ACCEPTS(b),
SELF.Str.ACCEPTS(b) && SELF.Int.ACCEPTS(b)))
}
class Foo { method Str { '' }; method Int { 1 } }
b 10000,
{
$ = old <1>, 1;
$ = old <1>, '1';
$ = old <1>, <1>;
$ = old <1>, Foo.new;
},
{
$ = new <1>, 1;
$ = new <1>, '1';
$ = new <1>, <1>;
$ = new <1>, Foo.new;
},
{
$ = <1>; $ = 1;
$ = <1>; $ = '1';
$ = <1>; $ = <1>;
$ = <1>; $ = Foo.new;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment