Re: Big speed difference between styles?



On 2006-09-16, roschler <robert.oschler@xxxxxxxxx> wrote:
I want to know what the performance difference is between the three
following styles of writing a predicate where I want to restrict the
domain of one of the arguments.

Suppose I have a predicate and I only want to accept three particular
values for the first argument. I could do the following:

% CASE 1 - use clause head unification
pred(value_one) :-
pred_aux(value_one).
pred(value_two) :-
pred_aux(value_one).
pred(value_three) :-
pred_aux(value_one).

pred_aux(X) :-
% do some more stuff with X
....

Or I could do:

% CASE 2 - use term comparison with disjunctions.
pred(X) :-
(X == value_one ; X == value_two; X == value_three),
% do some more stuff with X
...

Or I could do:

% CASE 3 - use the member function with a list of items.
pred(X) :-
member(X, [value_one, value_two, value_three]),
% do some more stuff with X

I'm guessing that CASE 2 and CASE 3 are slower than CASE 1 since
Prolog can't do a simple unification but has to examine rules or a list
instead. But how much slower? Sometimes it's much more convenient,
especially during prototyping, to just tweak a list of disjunctions or
a list used in a member/2 call, then to have to create extra predicate
clauses to control the domain of an argument.

You miss one option:

pred(X) :-
ok_for_aux(X),
pred_aux(value_one).

ok_for_aux(value_one).
ok_for_aux(value_two).
ok_for_aux(value_three).

This -in my opinion- is the best option. It is efficient (only very
slightly less than CASE 1), it avoids rewriting _and_ it gives you the
opportunity to give the test on X a name (ok_for_aux here).

Is the performance hit, if there is one, really worth worrying about?

Depends what you are doing. In general I'd advice to write using the
style you consider most readable. If the program gets too slot, run the
profiler to find the performance bottleneck and consider optimizing
there. Read "The Craft of Prolog" with many invaluable tips to keep your
code both readable and fast. In many cases these things do not bite in
Prolog. In cases where they do you can consider source rewriting using
term_expansion/2. That shouldn't be your first worry, but it is good to
know that if readability asks for a different representation than
performance you can opt for automatic transformation.

Cheers --- Jan
.



Relevant Pages

  • Re: Sublists question
    ... I do not really get what the predicate means or does. ... >>helps just to write the predicate out clearly in natural language, ... >>then translate into Prolog. ... more as a functional programming language than as a logic ...
    (comp.lang.prolog)
  • Re: Prolog module system
    ... I think prolog offers so much freedom that one can build its own ... Both modules define a member/2 predicate, ... this leads to a name conflict. ... shaky ground as current module systems (look at the issues with e.g. ...
    (comp.lang.prolog)
  • Re: Prolog, memory management and memory leaks
    ... predicate is executed that solves constraint problem. ... In some circumstances, this server leaks memory. ... "Understanding Memory Management in Prolog ... There is not just one garbage collector in Prolog. ...
    (comp.lang.prolog)
  • Re: Prolog module system
    ... my experiment with oop and modules has almost reach a point ... On the other hand, when I came to Prolog as a logic programming language, I ... Consider the length/2 predicate. ... OO systems have much more complex built-in lookup rules. ...
    (comp.lang.prolog)
  • Re: Prolog module system
    ... We talked a bit about this on the Prolog standardization forums but is ... Both modules define a member/2 predicate, ... this leads to a name conflict. ... shaky ground as current module systems (look at the issues with e.g. ...
    (comp.lang.prolog)