Big speed difference between styles?
- From: "roschler" <robert.oschler@xxxxxxxxx>
- Date: 16 Sep 2006 02:46:06 -0700
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.
Is the performance hit, if there is one, really worth worrying about?
Thanks.
.
- Follow-Ups:
- Re: Big speed difference between styles?
- From: bart demoen
- Re: Big speed difference between styles?
- From: Jan Wielemaker
- Re: Big speed difference between styles?
- Prev by Date: Re: The n-knights problem
- Next by Date: Re: The n-knights problem
- Previous by thread: SWI-Prolog : Easter egg for dolphin lovers?
- Next by thread: Re: Big speed difference between styles?
- Index(es):