[Shootout-list] perl && takfp

Greg Buchholz sleepingsquirrel@member.fsf.org
Mon, 13 Dec 2004 16:48:17 -0800 (PST)


--0-147854634-1102985297=:42501
Content-Type: text/plain; charset=us-ascii
Content-Id: 
Content-Disposition: inline

  With a naive implementation, perl exhausts all the memory on my
1GB machine for the "takfp" benchmark when n=10.  So I thought I'd
present two alternative implementations.  The first one can
compute the general tak function, while the second is optimized
for this specific benchmark.

Greg Buchholz





		
__________________________________ 
Do you Yahoo!? 
Send a seasonal email greeting and help others. Do good. 
http://celebrity.mail.yahoo.com
--0-147854634-1102985297=:42501
Content-Type: text/x-perl; name="takfp.pl"
Content-Description: takfp.pl
Content-Disposition: inline; filename="takfp.pl"

#!/usr/bin/perl -w 
# Perl version of floating point Tak function
# Greg Buchholz

$n=shift;
print tak(3*$n, 2*$n, $n)."\n";

sub tak
{   
    my $x=shift; my $y=shift; my $z=shift;
    
    my $tmp=$memo{$x}{$y}{$z};
    return $tmp if defined($tmp);
    
    if($y >= $x)
    {
        $memo{$x}{$y}{$z}=$z;
        return $z;
    }

    $memo{$x}{$y}{$z}=tak(tak($x-1,$y,$z),tak($y-1,$z,$x),tak($z-1,$x,$y)); 
    return $memo{$x}{$y}{$z};
}

--0-147854634-1102985297=:42501
Content-Type: text/x-perl; name="takfp2.pl"
Content-Description: takfp2.pl
Content-Disposition: inline; filename="takfp2.pl"

#!/usr/bin/perl -w 

$n=shift;
print takfp_benchmark($n)."\n";

sub takfp_benchmark
{   
    my $z=shift;

    return $z   if($z<0);
    return 2*$z if(int($z)==$z && $z%2);
    return $z+1 if(int($z)==$z && !($z%2));
    return 2*$z if(!(int($z)%2));
    return int($z)+2*($z-int($z));
}

--0-147854634-1102985297=:42501--