Re: if then else



On 2008-02-18, bart demoen <bmd@xxxxxxxxxxxxxx> wrote:

Yap SICS SWI B-Prolog | hProlog
orig 480 590 2980 484 | 360
meant 820 960 9919 730 | 264
equiv 1010 950 10300 1440 | 264

[don't take into account the hProlog figures at the moment]

What does this table show ?

1) in all systems, the programmer pays a hefty price for using
the equivalent form with if-then-else

2) even when the programmer expresses what she meant, she pays a
large performance penalty

This proves experimentally that Jan is right: if-then-else is not
necessarily faster than a clausal form, but the reason is because
(the above four) implementations don't do anything for if-then-else.

Now take also hProlog into account, and look at the orig row: all
these figures are in the same ball park (the timing for SWI is in line
with the usual perceived performance difference between SWI and other
systems - don't worry about it). Then why is hProlog so much faster
when if-then-else enters the picture ?

The answer is simple: only hProlog takes if-then-else serious, the
other four just dabble about a bit.

And it is no serious effort. When I started work on improving the VM
(the 5.7.x project), which did take care of if-then-else I was quickly
at a position where rewriting multiple clauses as if-then-else was in
the same leaque. This member for example was faster than the traditional
one:

member(E, [H|T]) :-
( E = H
; member(E, T)
).

Unfortunately this project is a bit lost in too many other tasks :-(

The interesting thing though is why implementers don't bother too much
about if-then-else? Is it the benchmarks that do not use them? Is it
considered bad style, so why bother making it fast? The latter was
surely my main motivation not to bother when I started SWI-Prolog around
1986. At some point I started realising that quite often it just fine or
even good and I enhanced the implementation considerably.

Curious is also that == is significantly slower than = on many systems?
Why is that? For SWI-Prolog it is slightly slower as it is merged with
the standard order comparison (passing a flag that asks whether or not
to answer order or just equivalence to avoid looking into constants).

That's the second issue important in the if-then-else debate:

Prolog implementations suck at if-then-else :-(

But this can be fixed :-)

Cheers --- Jan
.