This is surprising, interesting and pleasing...

There's some example NQP code to time calculating Fibonacci sequences.

I've tweaked it a tiny bit to take an optional count on the command line.
Unfortunately the NQP JVM prototype doesn't yet set @ARGS, so this isn't that
useful. Anyway, the code is:


$ cat ~/Perl/rakudo/nqp/examples/fib.nqp 
#! nqp

sub fib($n) {
    $n < 2 ?? $n !! fib($n-1) + fib($n - 2);
}

my $N := @ARGS > 1 ?? @ARGS[1] !! 29;

my $t0 := nqp::time_n();
my $z  := fib($N);
my $t1 := nqp::time_n();

nqp::say("fib($N) = " ~ fib($N));
nqp::say("time    = " ~ ($t1-$t0));


Running with NQP (optimised build):

$ nqp ~/Perl/rakudo/nqp/examples/fib.nqp
fib(29) = 514229
time    = 3.15967702865601

Running with the NQP JVM prototype:

$ nqp nqp-jvm-cc.nqp ~/Perl/rakudo/nqp/examples/fib.nqp
fib(29) = 514229
time    = 0.42100000381469727


That's about 8 times faster. (Even including all the compiling, right now
it's actually still faster. About 2.5 times faster)


And the equivalent Perl 5 code:

$ cat ~/test/fib.pl 
#! perl

use Time::HiRes 'time';

sub fib {
    my $n = shift;
    $n < 2 ? $n : fib($n-1) + fib($n - 2);
}

my $N = @ARGV > 1 ? $ARGV[0] : 29;

my $t0 = time;
my $z  = fib($N);
my $t1 = time;

print "fib($N) = " . fib($N) . "\n";
print "time    = " . ($t1-$t0) . "\n";


$ ~/Sandpit/5162/bin/perl5.16.2 ~/test/fib.pl 
fib(29) = 514229
time    = 0.544455051422119


So the NQP JVM prototype is about 1.5 faster at running that code than Perl
5. That's without any sort of tuning of its code generator.

That's quite impressive.

Well done Jonathan. Please don't stop :-)

Nicholas Clark

Reply via email to