[comp.lang.c] 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!"=/

debray@cs.arizona.edu (Saumya K. Debray) (06/13/90)

merlyn@iwarp.intel.com (Randal Schwartz) writes:
> 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.

Look -- a program either does something, or it doesn't [*].  To say that
it "most likely" does something is basically the same as saying 
"I {didn't bother to | couldn't} understand the code, but here's a
wild guess", except that it's not quite as honest.

For the benchmark in question, Van Roy posted both the C and the Prolog
sources, so it's easy to verify whether or not there's any table lookups
going on.  There isn't.

[*] Unless, I suppose, we're talking about randomization or true
    nondeterminism.  In this case, we aren't.

-- 
Saumya Debray		CS Department, University of Arizona, Tucson

     internet:   debray@cs.arizona.edu
     uucp:       uunet!arizona!debray