Skip to content

Instantly share code, notes, and snippets.

@zoffixznet
Created October 16, 2016 14:33
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/98dd51b90379373908b87118d9f7c391 to your computer and use it in GitHub Desktop.
Save zoffixznet/98dd51b90379373908b87118d9f7c391 to your computer and use it in GitHub Desktop.
use nqp;
sub timethese ($n, %what) {
my %results;
for %what.kv -> $name, &code {
my $start = now;
code for ^$n;
%results{$name} = now - $start;
}
%results;
}
say "starting";
my %res = timethese 200000, {
before => &before,
after => &after,
};
say "Before: %res<before>";
say " After: %res<after>";
printf " Diff: %.2f%%\n",
100*(%res<before> - %res<after>) / max(%res<before>,%res<after>);
sub infix:<foo1> (int $a, int $b) {
nqp::add_n($a, $b);
}
sub infix:<foo2> (int $a, int $b) returns int {
nqp::add_n($a, $b);
}
sub before {
(my int $ = 42) foo1 (my int $ = 42);
(my int $ = 42) foo1 (my int $ = 42);
(my int $ = 42) foo1 (my int $ = 42);
(my int $ = 42) foo1 (my int $ = 42);
(my int $ = 42) foo1 (my int $ = 42);
(my int $ = 42) foo1 (my int $ = 42);
(my int $ = 42) foo1 (my int $ = 42);
(my int $ = 42) foo1 (my int $ = 42);
(my int $ = 42) foo1 (my int $ = 42);
(my int $ = 42) foo1 (my int $ = 42);
(my int $ = 42) foo1 (my int $ = 42);
(my int $ = 42) foo1 (my int $ = 42);
(my int $ = 42) foo1 (my int $ = 42);
}
sub after {
(my int $ = 42) foo2 (my int $ = 42);
(my int $ = 42) foo2 (my int $ = 42);
(my int $ = 42) foo2 (my int $ = 42);
(my int $ = 42) foo2 (my int $ = 42);
(my int $ = 42) foo2 (my int $ = 42);
(my int $ = 42) foo2 (my int $ = 42);
(my int $ = 42) foo2 (my int $ = 42);
(my int $ = 42) foo2 (my int $ = 42);
(my int $ = 42) foo2 (my int $ = 42);
(my int $ = 42) foo2 (my int $ = 42);
(my int $ = 42) foo2 (my int $ = 42);
(my int $ = 42) foo2 (my int $ = 42);
(my int $ = 42) foo2 (my int $ = 42);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment