Proof that Prolog can be faster than C
Peter Van Roy
vanroy at pisces
Sat Jun 16 08:09:22 AEST 1990
In article <1990Jun15.205242.16314 at agate.berkeley.edu> dankg at volcano.Berkeley.EDU (Dan KoGai) writes:
> But this C code is hardly optimum. 1st of all you don't need
>a1, a2, a3 to waste space and time. try the following.
>
>int tak(int x, int y, int z)
>{
> return (x <= y) ? z : tak(tak(--x, y, z),tak(--y, z, x),tak(--z, x, y))
>
>}
>
>> [Prolog version omitted]
>
>____ __ __ + Dan The Walking C Obfscunator
Did you try to run this program?
It behaves very strangely:
(after adding a semicolon to the return statement)
mammoth:~/Peter <20> cc -g taktak.c
mammoth:~/Peter <21> dbx a.out
dbx version 2.0
Copyright 1988 MIPS Computer Systems Inc.
Type 'help' for help.
reading symbolic information ...
main: 15 printf("%d\n", tak(24, 16, 8));
(dbx) run
^C <- interrupted after ~10 seconds
Interrupt [tak:10 +0x28,0x400220]
tak(--z, x, y));
(dbx) print x,y,z
-44896 -44897 -44898 <- strange values
>From page 186 of K&R:
(note that predecrement does a side-effect)
[Regarding function evaluation] The order of evaluation of arguments
is undefined by the language; take note that the various compilers
differ.
By the way, I did try a different version of tak.c without the temporary
variables a1, a2, and a3. It ran *slower* unoptimized and at exactly the
same speed optimized.
Please think before you post.
Sincerely,
Peter Van Roy
More information about the Comp.lang.c
mailing list