Skip to content

Instantly share code, notes, and snippets.

@masak
Created July 2, 2011 19:29
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 masak/1061562 to your computer and use it in GitHub Desktop.
Save masak/1061562 to your computer and use it in GitHub Desktop.
Conversation with a French teacher about Rakudo performance

I just find the answer.

If works.

lets see two exécutions of the same program, the first in perl 5 with tis exec time :

The perl 5 program.

#!/usr/bin/perl
sub ack {
 my ($m,$n) = @_;
 return ($n+1) if $m == 0;
 return ack($m-1, 1) if $n == 0;
 print $i++, "\n";  # This is to trace the process.
 return ack($m-1, ack($m, $n-1));
}
$r = ack(3,5);
print ("resultat : $r.\n");

Its execution.

resultat : 253.

real    0m0.183s
user    0m0.095s <------
sys    0m0.042s

now in perl6 with multisub

The perl 6 program.

use v6;
multi sub ack (0,$n) {return ($n+1)};
multi sub ack ($m, 0) {return ack($m-1, 1)};
# "say $i++;" is to trace the process.
multi sub ack ($m,$n) {say $i++;return ack($m-1, ack($m, $n-1))};
my $i;
my $r = &ack(3,5);
say ("resultat : $r.");

resultat : 253.

real    1m7.513s
user    1m4.085s <------
sys    0m1.525s

In this case the exec time has been multiplied by 16 !

Wirh the call ack(3,8) in perl 5, it needs 1391980 calls to obtain the result 2045. in a user time of 0m6.471s

In perl 6, I did trace it to see that everything was xorking, but, after àne hour I was only at half ot the itération process. It meens that the êrl6 program needs more than 2 hours to reach the result.

Regards.

[footer elided]

So, the basic problem is "it's correct but slow"?

Here are my benchmarks with the same scripts. Perl 5:

resultat : 253.

real    0m0.411s
user    0m0.080s
sys     0m0.080s

And Perl 6:

resultat : 253.

real    1m59.028s
user    1m43.090s
sys     0m0.650s

I seem to be on a slower setup than you. I'm running in a VM on a laptop, so that doesn't surprise me much.

Yes, Rakudo is slow currently. The focus up until 2010 has been on features, not on speed. In 2011, efforts have switched over to improving performance. Most of the fruits of that labour are still in a branch called "nom" (which I mentioned in the first of my talks).

Here is the benchmark when running the script under nom.

resultat : 253.

real    1m6.424s
user    0m53.930s
sys     0m0.610s

(If you try to reproduce this at home you'll currently need to add the line proto sub ack ($m, $n) { * } at the top of the script. It won't be necessary in the long run, but it is currently due to limitations in nom.)

So, that's a halving of the time required. If I remove the "say" in the 'ack' function, I get:

resultat : 253.

real    0m9.736s
user    0m9.240s
sys     0m0.250s

...which tells us that IO is currently a big bottleneck, corresponding to about 80% of the time spent in the p6 program. (Corresponding to about 15% in the Perl 5 program.)

Finally, I would suggest you're comparing apples to oranges by doing if statements in Perl 5 and multi subs in Perl 6. A multi sub is not an if statement, and (even with way clever optimizations) will probably never be as fast as one. Here, I rewrote the Perl 6 script to mimic the Perl 5 one:

use v6;

sub ack($m, $n) {
   return $n + 1         if $m == 0;
   return ack($m - 1, 1) if $n == 0;
   return ack($m - 1, ack($m, $n - 1));
}

my $r = &ack(3,5);
say ("resultat : $r.");

Now I get this with Rakudo master:

resultat : 253.

real    0m11.350s
user    0m10.160s
sys     0m0.240s

And this with Rakudo nom:

resultat : 253.

real    0m2.506s
user    0m2.420s
sys     0m0.040s

That's still 3 times as slow as Perl 5, but it's a lot more promising, I think. Especially since there's room for a number of optimizations with nom as a baseline.

Hope that helps.

Regards, // Carl

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