[Wellington-pm] Perl 5 faster than C

Douglas Bagnall douglas at paradise.net.nz
Tue Mar 14 18:21:44 PST 2006


... with Memoize

Just to pick up on something else Grant mentioned last night.

--------

#!/usr/bin/perl

use Memoize;

#cut, paste, search, replace from C version -- no perlish optimisation
sub ack {
     my ($m, $n) = @_;
     return ($m ? (ack($m - 1, $n ? ack($m, ($n - 1)) : 1)) : $n + 1);
}

memoize('ack');

print ack(3, 11);

--------


$ time perl ack.pl
16381
real    0m0.810s
user    0m0.736s
sys     0m0.024s


$ time ./ack34 11  #the C version, compiled with gcc-3.4
Ack(3,11): 16381

real    0m1.802s
user    0m1.752s
sys     0m0.004s

$ time perl ack-no-memoize.pl
  # (cup of tea time)
  # arrive back to sound of swapping
  # ^C^C
real    8m31.296s
user    6m37.961s
sys     0m4.268s




douglas


More information about the Wellington-pm mailing list