[comp.lang.perl] Bogus!

merlyn@iwarp.intel.com (Randal Schwartz) (06/13/90)

In article <36986@ucbvax.BERKELEY.EDU>, vanroy@pisces writes:
| 
| Benchmark timings:
| 
| Benchmark	Prolog  	C (no opt.)	C (opt. 4)
| 
| tak(24,16,8)    1.2		2.1		1.6

Bogus.

Comparing apples and oranges dude.

The prolog version most likely saves results from tak(a,b,c) so that
they can do a quick lookup the next time around.  I jimmied up one
in perl that does the same thing...

$ cat tak
#!/local/usr/bin/perl

sub tak {
	$takcalled++;
	local($x,$y,$z) = @_;
	return $z if $x <= $y;
	if (defined $tak{$x,$y,$z}) {
		$takrecalled++;
		# print "recomputing $x $y $z\n";
		return $tak{$x,$y,$z};
	}
	local($a1,$a2,$a3);
	$a1 = &tak($x-1,$y+0,$z+0);
	$a2 = &tak($y-1,$z+0,$x+0);
	$a3 = &tak($z-1,$x+0,$y+0);
	return $tak{$x,$y,$z} = &tak($a1+0,$a2+0,$a3+0);
}

$start = (times)[0];

$result = &tak(24,16,8);

$finish = (times)[0];

$seconds = $finish - $start;

print	"result $result, ",
	"called $takcalled, ",
	"recalled $takrecalled, ",
	"time $seconds secs\n";
$ ./tak
result 9, called 941, recalled 194, time 0.91666666666666662966 secs

Now, tell me if 0.917 seconds of user time isn't better than either C
or prolog!  (This is on a sparc..., so there's some more apples and
oranges for ya :-)

Notice how many *recalled* short-circuits I took.  The C code would
have had to have computed that out fully.  But once prolog "knew" the
fact of tak(11,14,17,96) (or whatever), it'd never compute it again.

Stop comparing apples and oranges, unless you are willing to throw in
a few Perls. :-)

(Something with a little less smoke and mirrors is to compare timings
on a factorial written with recursion.  Trace it through slowly, and
you'll see what I mean.)

Just another Perl hacker,
-- 
/=Randal L. Schwartz, Stonehenge Consulting Services (503)777-0095 ==========\
| on contract to Intel's iWarp project, Beaverton, Oregon, USA, Sol III      |
| merlyn@iwarp.intel.com ...!any-MX-mailer-like-uunet!iwarp.intel.com!merlyn |
\=Cute Quote: "Welcome to Portland, Oregon, home of the California Raisins!"=/