On Sat, Feb 02, 2013 at 06:32:10PM -0800, Matthew Wilson wrote:
> Did you mean to use $z in the say output of the nqp and perl versions of
> the microbenchmark, or did you mean to run it twice?
I didn't write the nqp microbenchmark, and I simply transcribed it to
Perl 5, so it's intentional. I didn't spot it, thanks for noticing.
It doesn't actually change the numbers, because the timing is still for one
call. But it does cause the program to do a lot less work to get there.
$ 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) = $z");
nqp::say("time = " ~ ($t1-$t0));
$ nqp ~/Perl/rakudo/nqp/examples/fib.nqpfib(29) = 514229
time = 3.2875030040741
$ nqp nqp-jvm-cc.nqp ~/Perl/rakudo/nqp/examples/fib.nqp
fib(29) = 514229
time = 0.39100003242492676
and Perl 5:
$ 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) = $z\n";
print "time = " . ($t1-$t0) . "\n";
$ ~/Sandpit/5162/bin/perl5.16.2 ~/test/fib.pl
fib(29) = 514229
time = 0.5503830909729
Nicholas Clark