Re: Big speed difference between styles?



On Sat, 16 Sep 2006 03:46:06 -0700, roschler 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.

When you want to know something, and be (reasonably) sure you know
that you know something, it is worth conducting the experiment yourself.
Here is a program you can execute on your favourite Prolog system, and see
for yourself ...
The query is ?- t(1000000).
I will post results in different Prolog systems later. But only after you
have posted your results, and commented on them: do ut des.

Cheers

Bart Demoen



pred1(value_one).
pred1(value_two).
pred1(value_three).

pred2(X) :- (X == value_one ; X == value_two; X == value_three), !.


pred3(X) :- mmember(X, [value_one, value_two, value_three]), !.


pred4(_).


mmember(X,[X|_]).
mmember(X,[_|R]) :- mmember(X,R).

t(N) :-
statistics(runtime,[T1|_]),
do1(N),
statistics(runtime,[T2|_]),
T is T2 - T1,
write(method1 = T), nl, fail.
t(N) :-
statistics(runtime,[T1|_]),
do2(N),
statistics(runtime,[T2|_]),
T is T2 - T1,
write(method2 = T), nl, fail.
t(N) :-
statistics(runtime,[T1|_]),
do3(N),
statistics(runtime,[T2|_]),
T is T2 - T1,
write(method3 = T), nl, fail.
t(N) :-
statistics(runtime,[T1|_]),
do4(N),
statistics(runtime,[T2|_]),
T is T2 - T1,
write(dummy=T), nl, fail.


do1(N) :-
(N > 0 ->
pred1(value_one),
pred1(value_two),
pred1(value_three),
M is N - 1,
do1(M)
;
true
).

do2(N) :-
(N > 0 ->
pred2(value_one),
pred2(value_two),
pred2(value_three),
M is N - 1,
do2(M)
;
true
).

do3(N) :-
(N > 0 ->
pred3(value_one),
pred3(value_two),
pred3(value_three),
M is N - 1,
do3(M)
;
true
).

do4(N) :-
(N > 0 ->
pred4(value_one),
pred4(value_two),
pred4(value_three),
M is N - 1,
do4(M)
;
true
).



.



Relevant Pages

  • Re: Alternative to backtracking
    ... Sorry if this appears as a duplicate post, ... called chronological backtracking. ... In other words a predicate would fail either if there ...
    (comp.lang.prolog)
  • prolog assignment
    ... Define a predicate lastthat takes two arguments, an element X ... grape, kiwi]. ... predicate will succeed if S is a subset of L, otherwise it will fail. ... Define a predicate intersection that takes three sets L1, ...
    (comp.lang.prolog)
  • Re: mErge
    ... On Tue, 6 Sep 2005, mAsterdam wrote: ... Nothing you wrote suggested your mErge predicate should fail at all. ... Matthew Huntbach ...
    (comp.lang.prolog)
  • Re: How to think?
    ... you do not need to explicitly return #f (you just let the predicate ... The steps you describe will never fail you and is, IMHO, how everybody ... reasons. ... Prev by Date: ...
    (comp.lang.scheme)