Skip to content

Instantly share code, notes, and snippets.

@timo
Created October 26, 2014 21:21
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 timo/2d2cc1260428e5758086 to your computer and use it in GitHub Desktop.
Save timo/2d2cc1260428e5758086 to your computer and use it in GitHub Desktop.
patch to make formula-evaluation-benchmark a bunch faster by using native ints and Num instead of Rat
commit 4e7d715a562c9101df423458d8f2d619c02ab9ee
Author: Timo Paulssen <timonator@perpetuum-immobile.de>
Date: Sun Oct 26 22:13:28 2014 +0100
sprinkle the code with native variables
diff --git a/perl6/ast.pl b/perl6/ast.pl
index a083444..416f83d 100644
--- a/perl6/ast.pl
+++ b/perl6/ast.pl
@@ -18,14 +18,14 @@ my %functions = (
},
$DEVIDE => sub (@args) {
- return @args[0] / @args[1];
+ return @args[0].Num / @args[1];
},
$SUM => sub (@args) {
- my $sum = 0;
+ my int $sum = 0;
for @args -> $arg {
- $sum += $arg;
+ $sum = $sum + $arg.Int;
}
return $sum;
@@ -33,16 +33,17 @@ my %functions = (
);
sub evaluate_ast(@ast) {
- my $oper = @ast[0];
+ my int $oper = @ast[0];
my $func = %functions{$oper};
my @evaluated_args;
+ my int $astsize = @ast.elems;
- loop ( my $i = 1; $i < @ast.elems; $i++ ) {
+ loop ( my int $i = 1; $i < $astsize; $i = $i + 1 ) {
if ( @ast[$i] ~~ Array ) {
- @evaluated_args.push( evaluate_ast( @ast[$i] ) );
+ @evaluated_args.push( evaluate_ast( @ast.at_pos($i) ) );
} else {
- @evaluated_args.push( @ast[$i] );
+ @evaluated_args.push( @ast.at_pos($i) );
}
}
@@ -50,16 +51,16 @@ sub evaluate_ast(@ast) {
}
sub timeAST(@ast) {
- my $t0 = time;
+ my int $t0 = time;
- my $sum = 0;
- my $iterations = 100_000;
+ my int $sum = 0;
+ my int $iterations = 100_000;
for (1..$iterations) {
- $sum += evaluate_ast(@ast);
+ $sum = $sum + evaluate_ast(@ast);
}
- my $compute_time = time - $t0;
+ my int $compute_time = time - $t0;
say "COMPUTED [$iterations] ITERATIONS IN [$compute_time] SECONDS";
@koorchik
Copy link

my int $sum = 0;

It happen occasionally that SUM is integer.

change

 [ SUBSTRACT,[ADD,[DIVIDE,[MULTIPLY,10,20],30],40],50],

to

 [ SUBSTRACT,[ADD,[DIVIDE,[MULTIPLY,20,20],30],40],50],

And the sum will be 4566666.666663495.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment