These are the notes for the Efficiency issues in Prolog part of the Computational Logic course. The slides and notes go essentially in sync, with these notes providing the explanations, reasoning, and some side comments.
How to use this interactive document:
The code boxes can be edited and run in place. Feel free to edit any program and make all the queries you want!
The general advice is the general advice of every language:
Otherwise use structures — that is, functors — which need less memory and give direct access to each argument through arg/3, exactly like an array.
Compare the two pictures. A list of n elements is n cons cells, each holding an element and a pointer to the next one; reaching element k means following k pointers. A structure of arity n is one cell with n slots; reaching argument k is one indexed access.
And use advanced data structures where they fit: sorted trees, incomplete structures (such as the difference lists that we used for sorting and for parsing), nested structures, and so on.
D. H. D. Warren: "Prolog means easy pointers".
Here is swapping the two arguments of a structure, from f(X,Y) to f(Y,X), written the way somebody coming from another language might write it (exaggerating a little). It is slow, long, and difficult to understand:
:- module(_,_).
swap_slow(S1, S2) :-
functor(S1, f, 2),
functor(S2, f, 2),
arg(1, S1, X1),
arg(2, S1, Y1),
arg(1, S2, X2),
arg(2, S2, Y2),
X1 = Y2,
X2 = Y1.And this is the fast, intuitive, shorter version:swap(f(X, Y), f(Y, X)).
?- swap_slow(f(a,b),S).Expected answer:
S = f(b,a) ?
?- swap(f(a,b),S).Expected answer:
S = f(b,a) ?The second version is one line, and it is easier to read. The first one takes the structures apart by hand and then puts them back together: eight goals to say what head unification says by itself.
And it is faster. To see by how much we swap a million times with each version: ten swaps in each of 100,000 iterations. swaps_loop/1 is the same loop with no swaps in it, so that the time of the loop itself can be subtracted:
swaps_slow(0).
swaps_slow(N) :-
N > 0,
swap_slow(f(a,b), _),
swap_slow(f(a,b), _),
swap_slow(f(a,b), _),
swap_slow(f(a,b), _),
swap_slow(f(a,b), _),
swap_slow(f(a,b), _),
swap_slow(f(a,b), _),
swap_slow(f(a,b), _),
swap_slow(f(a,b), _),
swap_slow(f(a,b), _),
N1 is N - 1,
swaps_slow(N1).
swaps(0).
swaps(N) :-
N > 0,
swap(f(a,b), _),
swap(f(a,b), _),
swap(f(a,b), _),
swap(f(a,b), _),
swap(f(a,b), _),
swap(f(a,b), _),
swap(f(a,b), _),
swap(f(a,b), _),
swap(f(a,b), _),
swap(f(a,b), _),
N1 is N - 1,
swaps(N1).
swaps_loop(0).
swaps_loop(N) :-
N > 0,
N1 is N - 1,
swaps_loop(N1).?- speedup_loop(swaps_loop(100000), swaps_slow(100000), swaps(100000)).Expected answer (the times change from run to run):
Slow: 56.2941 ms, fast: 17.5208 ms, speedup: 3.2 yesThe short version is about three times faster.
speedup(Slow, Fast) :-
speedup_loop(true, Slow, Fast).
speedup_loop(Loop, Slow, Fast) :-
time_of(Loop, Slow, TS),
time_of(Loop, Fast, TF),
show_speedup(TS, TF).
time_of(Loop, Goal, T) :-
repeat_time(1, Loop, Goal, T).
repeat_time(N, Loop, Goal, T) :-
loop_time(N, Goal, TG),
( TG >= 100
-> loop_time(N, Loop, TL),
( TG - TL > TG / 10 % (else too fast to tell
-> T is (TG - TL) / N % from the loop itself)
; T = 0
)
; N1 is N * 10,
repeat_time(N1, Loop, Goal, T)
).
loop_time(N, Goal, T) :-
statistics(runtime, [T0, _]),
( between(1, N, _), once(Goal), fail ; true ),
statistics(runtime, [T1, _]),
T is T1 - T0.
show_speedup(TS, TF) :-
format("Slow: ", []), show_time(TS),
format(", fast: ", []), show_time(TF),
( TS > 0, TF > 0
-> S is TS / TF,
format(", speedup: ~1f~n", [S])
; nl
).
show_time(T) :-
T > 0, !,
format("~4f ms", [T]).
show_time(_) :-
format("too fast to measure", []).
:- use_module(library(between)).The other example files contain a copy of these predicates, which is not shown. :- module(_,_).
three_elements_weak(L) :-
length(L, N),
N = 3.And this is a better one:three_elements([_,_,_]).
?- three_elements_weak([a,b,c]).Expected answer:
yes
?- three_elements([a,b,c]).Expected answer:
yes
?- three_elements_weak([a,b]).Expected answer:
no
?- three_elements([a,b]).Expected answer:
noThe weak version always traverses the whole list and computes its length, however long the list is, only to compare the result with 3. The good version is a single head unification, which looks at three cons cells and stops. On a list of a million elements the difference is a million to three.
We can see it in the times for a list of N elements: that of the weak answer grows with N, while the better one is too fast to measure:
test(N) :-
numbers(N, L),
speedup(three_elements_weak(L), three_elements(L)).%! \begin{focus}
numbers(0, []).
numbers(N, [N|Ns]) :-
N > 0,
N1 is N - 1,
numbers(N1, Ns).
%! \end{focus}
speedup(Slow, Fast) :-
speedup_loop(true, Slow, Fast).
speedup_loop(Loop, Slow, Fast) :-
time_of(Loop, Slow, TS),
time_of(Loop, Fast, TF),
show_speedup(TS, TF).
time_of(Loop, Goal, T) :-
repeat_time(1, Loop, Goal, T).
repeat_time(N, Loop, Goal, T) :-
loop_time(N, Goal, TG),
( TG >= 100
-> loop_time(N, Loop, TL),
( TG - TL > TG / 10 % (else too fast to tell
-> T is (TG - TL) / N % from the loop itself)
; T = 0
)
; N1 is N * 10,
repeat_time(N1, Loop, Goal, T)
).
loop_time(N, Goal, T) :-
statistics(runtime, [T0, _]),
( between(1, N, _), once(Goal), fail ; true ),
statistics(runtime, [T1, _]),
T is T1 - T0.
show_speedup(TS, TF) :-
format("Slow: ", []), show_time(TS),
format(", fast: ", []), show_time(TF),
( TS > 0, TF > 0
-> S is TS / TF,
format(", speedup: ~1f~n", [S])
; nl
).
show_time(T) :-
T > 0, !,
format("~4f ms", [T]).
show_time(_) :-
format("too fast to measure", []).
:- use_module(library(between)).?- test(10000).Expected answer (the times change from run to run):
Slow: 0.0603 ms, fast: too fast to measure yes
?- test(1000000).Expected answer (the times change from run to run):
Slow: 6.7334 ms, fast: too fast to measure yes
:- module(_,_).
:- dynamic counting/1.
bad_count(N) :-
assert(counting(N)),
even_worse.
even_worse :-
retract(counting(0)).
even_worse :-
retract(counting(N)),
N > 0,
N1 is N - 1,
assert(counting(N1)),
even_worse.and this one passes the counter as an argument:%! \begin{focus}
good_count(0).
good_count(N) :-
N > 0,
N1 is N - 1,
good_count(N1).
%! \end{focus}
speedup(Slow, Fast) :-
speedup_loop(true, Slow, Fast).
speedup_loop(Loop, Slow, Fast) :-
time_of(Loop, Slow, TS),
time_of(Loop, Fast, TF),
show_speedup(TS, TF).
time_of(Loop, Goal, T) :-
repeat_time(1, Loop, Goal, T).
repeat_time(N, Loop, Goal, T) :-
loop_time(N, Goal, TG),
( TG >= 100
-> loop_time(N, Loop, TL),
( TG - TL > TG / 10 % (else too fast to tell
-> T is (TG - TL) / N % from the loop itself)
; T = 0
)
; N1 is N * 10,
repeat_time(N1, Loop, Goal, T)
).
loop_time(N, Goal, T) :-
statistics(runtime, [T0, _]),
( between(1, N, _), once(Goal), fail ; true ),
statistics(runtime, [T1, _]),
T is T1 - T0.
show_speedup(TS, TF) :-
format("Slow: ", []), show_time(TS),
format(", fast: ", []), show_time(TF),
( TS > 0, TF > 0
-> S is TS / TF,
format(", speedup: ~1f~n", [S])
; nl
).
show_time(T) :-
T > 0, !,
format("~4f ms", [T]).
show_time(_) :-
format("too fast to measure", []).
:- use_module(library(between)).Both count down from N. The first one keeps the counter in the database, retracting it and asserting it again in every iteration; the second one passes it in an argument, which is what arguments are for.?- speedup(bad_count(10000), good_count(10000)).Expected answer (the times change from run to run):
Slow: 6.0375 ms, fast: 0.1294 ms, speedup: 46.6 yes
?- speedup(bad_count(100000), good_count(100000)).Expected answer (the times change from run to run):
Slow: 62.0381 ms, fast: 1.3390 ms, speedup: 46.3 yesgood_count/1 is over 40 times faster than bad_count/1, and it uses virtually no memory: counting down from a million, bad_count/1 needs some 65 MB, and good_count/1 nothing that can be measured. Every assert/1 and retract/1 is a modification of the program itself, and the system cannot optimize what it cannot predict.
:- module(_,_).
fib(0, 0).
fib(1, 1).
fib(N, F) :-
N > 1,
N1 is N - 1,
N2 is N1 - 1,
fib(N1, F1),
fib(N2, F2),
F is F1 + F2.Storing each result when it is found turns that into a linear walk::- dynamic lemma_fib/2.
lemma_fib(0, 0).
lemma_fib(1, 1).
lfib(N, F) :-
lemma_fib(N, F),
!.
lfib(N, F) :-
N > 1,
N1 is N - 1,
N2 is N1 - 1,
lfib(N1, F1),
lfib(N2, F2),
F is F1 + F2,
assert(lemma_fib(N, F)).To measure lfib/2 from scratch, the table has to be emptied first:%! \begin{focus}
lfib_fresh(N, F) :-
retractall(lemma_fib(_, _)),
assert(lemma_fib(0, 0)),
assert(lemma_fib(1, 1)),
lfib(N, F).
%! \end{focus}
speedup(Slow, Fast) :-
speedup_loop(true, Slow, Fast).
speedup_loop(Loop, Slow, Fast) :-
time_of(Loop, Slow, TS),
time_of(Loop, Fast, TF),
show_speedup(TS, TF).
time_of(Loop, Goal, T) :-
repeat_time(1, Loop, Goal, T).
repeat_time(N, Loop, Goal, T) :-
loop_time(N, Goal, TG),
( TG >= 100
-> loop_time(N, Loop, TL),
( TG - TL > TG / 10 % (else too fast to tell
-> T is (TG - TL) / N % from the loop itself)
; T = 0
)
; N1 is N * 10,
repeat_time(N1, Loop, Goal, T)
).
loop_time(N, Goal, T) :-
statistics(runtime, [T0, _]),
( between(1, N, _), once(Goal), fail ; true ),
statistics(runtime, [T1, _]),
T is T1 - T0.
show_speedup(TS, TF) :-
format("Slow: ", []), show_time(TS),
format(", fast: ", []), show_time(TF),
( TS > 0, TF > 0
-> S is TS / TF,
format(", speedup: ~1f~n", [S])
; nl
).
show_time(T) :-
T > 0, !,
format("~4f ms", [T]).
show_time(_) :-
format("too fast to measure", []).
:- use_module(library(between)).?- speedup(fib(24,_), lfib_fresh(24,_)).Expected answer (the times change from run to run):
Slow: 4.1585 ms, fast: 0.0056 ms, speedup: 740.9 yesThe speedup grows with N:
?- speedup(fib(30,_), lfib_fresh(30,_)).Expected answer (the times change from run to run):
Slow: 74.3170 ms, fast: 0.0093 ms, speedup: 8030.5 yesAnd if the table is not emptied, lfib/2 just finds the result in it:
?- speedup(fib(24,_), lfib(24,_)).Expected answer (the times change from run to run):
Slow: 4.0951 ms, fast: too fast to measure yeslfib(24,F) is several hundred times faster than fib(24,F), and takes almost no time if it is called again, because the answer is already stored. It also uses far less memory: fib(24,F) needs some 25 MB, and lfib(24,F) nothing that can be measured.
Two suggestions follow.
Do not keep alternatives if they are not needed. The cut in the first clause of member_check/2 turns a generator into a test:
:- module(_,_). :- use_module(library(between)). member_check([X|_],X) :- !. member_check([_|Xs],X) :- member_check(Xs,X).The library member/2 finds b twice in this list, while member_check/2 stops at the first occurrence:
?- member(b,[a,b,c,b]), write(found), nl, fail.Expected answer:
found found no
?- member_check([a,b,c,b],b), write(found), nl, fail.Expected answer:
found noProgram deterministic problems in a deterministic way. As an example, let us decompose N into two summands:
decomp_simplistic(N, S1, S2) :-
between(0, N, S1),
between(0, N, S2),
N =:= S1 + S2.
decomp(N, S1, S2) :-
between(0, N, S1),
S2 is N - S1.?- decomp_simplistic(3,S1,S2).Expected answer:
S1 = 0, S2 = 3 ? ; S1 = 1, S2 = 2 ? ; S1 = 2, S2 = 1 ? ; S1 = 3, S2 = 0 ? ; no
?- decomp(3,S1,S2).Expected answer:
S1 = 0, S2 = 3 ? ; S1 = 1, S2 = 2 ? ; S1 = 2, S2 = 1 ? ; S1 = 3, S2 = 0 ? ; noBoth give the same four answers for N = 3. But decomp_simplistic/3 generates and tests: it tries every one of the (N+1)² pairs and checks each of them, so it is quadratic. decomp/3 generates only S1 and computes S2, so it is linear — and it never creates a choice point that is going to fail.
To compare them we find all the solutions with each one, using a failure-driven loop:
%! \begin{focus}
all(Goal) :-
call(Goal),
fail.
all(_).
%! \end{focus}
speedup(Slow, Fast) :-
speedup_loop(true, Slow, Fast).
speedup_loop(Loop, Slow, Fast) :-
time_of(Loop, Slow, TS),
time_of(Loop, Fast, TF),
show_speedup(TS, TF).
time_of(Loop, Goal, T) :-
repeat_time(1, Loop, Goal, T).
repeat_time(N, Loop, Goal, T) :-
loop_time(N, Goal, TG),
( TG >= 100
-> loop_time(N, Loop, TL),
( TG - TL > TG / 10 % (else too fast to tell
-> T is (TG - TL) / N % from the loop itself)
; T = 0
)
; N1 is N * 10,
repeat_time(N1, Loop, Goal, T)
).
loop_time(N, Goal, T) :-
statistics(runtime, [T0, _]),
( between(1, N, _), once(Goal), fail ; true ),
statistics(runtime, [T1, _]),
T is T1 - T0.
show_speedup(TS, TF) :-
format("Slow: ", []), show_time(TS),
format(", fast: ", []), show_time(TF),
( TS > 0, TF > 0
-> S is TS / TF,
format(", speedup: ~1f~n", [S])
; nl
).
show_time(T) :-
T > 0, !,
format("~4f ms", [T]).
show_time(_) :-
format("too fast to measure", []).
:- use_module(library(between)).?- speedup(all(decomp_simplistic(1000,_,_)), all(decomp(1000,_,_))).Expected answer (the times change from run to run):
Slow: 31.7157 ms, fast: 0.0346 ms, speedup: 916.9 yes
?- speedup(all(decomp_simplistic(3000,_,_)), all(decomp(3000,_,_))).Expected answer (the times change from run to run):
Slow: 280.5960 ms, fast: 0.1186 ms, speedup: 2365.8 yesThe speedup grows with N, as it must when one version is quadratic and the other one linear.
:- module(_,_).
same_elements_naive(L1, L2) :-
\+ (member(X, L1), \+ member(X, L2)),
\+ (member(X, L2), \+ member(X, L1)).The other possibility is to sort and unify:same_elements(L1, L2) :-
sort(L1, Sorted),
sort(L2, Sorted).?- same_elements_naive([a,b,c],[c,a,b]).Expected answer:
yes
?- same_elements([a,b,c],[c,a,b]).Expected answer:
yesThe naive version is quadratic: for each element of one list it searches the other one. The second version sorts both lists, which can be done in O(N log N), and then does one unification of the two results, which is where the real elegance is: two sorted lists (without repetitions) contain the same elements exactly when they are the same term.
For the comparison we use lists with the numbers from 1 to N in scrambled order (N must not be a multiple of 7919):
test(N) :-
scrambled(N, L),
reverse(L, R),
speedup(same_elements_naive(L,R), same_elements(L,R)).%! \begin{focus}
scrambled(N, L) :-
scrambled_from(0, N, L).
scrambled_from(N, N, []) :- !.
scrambled_from(I, N, [X|Xs]) :-
X is (I * 7919) mod N + 1,
I1 is I + 1,
scrambled_from(I1, N, Xs).
%! \end{focus}
speedup(Slow, Fast) :-
speedup_loop(true, Slow, Fast).
speedup_loop(Loop, Slow, Fast) :-
time_of(Loop, Slow, TS),
time_of(Loop, Fast, TF),
show_speedup(TS, TF).
time_of(Loop, Goal, T) :-
repeat_time(1, Loop, Goal, T).
repeat_time(N, Loop, Goal, T) :-
loop_time(N, Goal, TG),
( TG >= 100
-> loop_time(N, Loop, TL),
( TG - TL > TG / 10 % (else too fast to tell
-> T is (TG - TL) / N % from the loop itself)
; T = 0
)
; N1 is N * 10,
repeat_time(N1, Loop, Goal, T)
).
loop_time(N, Goal, T) :-
statistics(runtime, [T0, _]),
( between(1, N, _), once(Goal), fail ; true ),
statistics(runtime, [T1, _]),
T is T1 - T0.
show_speedup(TS, TF) :-
format("Slow: ", []), show_time(TS),
format(", fast: ", []), show_time(TF),
( TS > 0, TF > 0
-> S is TS / TF,
format(", speedup: ~1f~n", [S])
; nl
).
show_time(T) :-
T > 0, !,
format("~4f ms", [T]).
show_time(_) :-
format("too fast to measure", []).
:- use_module(library(between)).?- test(1000).Expected answer (the times change from run to run):
Slow: 13.8855 ms, fast: 0.0434 ms, speedup: 320.2 yes
?- test(5000).Expected answer (the times change from run to run):
Slow: 327.1890 ms, fast: 0.3058 ms, speedup: 1070.0 yesWith 1000 elements, sorting and unifying is several hundred times faster, and the speedup grows with the size of the lists.
The way to do it is to reorder the goals in the body (possibly even dynamically). Take a generate-and-test program written in the natural, but wrong, order:
generate_z(Z) :- generate_x(X), generate_y(X, Y), test_x(X), test_y(Y), combine(X, Y, Z).Every Y is generated for every X, including the values of X that test_x/1 is about to reject. So we perform the tests as soon as possible:
generate_z(Z) :- generate_x(X), test_x(X), generate_y(X, Y), test_y(Y), combine(X, Y, Z).Even better is to test as deeply as possible within the generator, so that a bad candidate is never even produced:
generate_z(Z) :- generate_x_test(X), generate_y_test(X, Y), combine(X, Y, Z).
Two things follow, and the second one matters more than it seems:
:- module(_,_). bad_greater(_X,[]). bad_greater(X,[Y|Ys]) :- X > Y, bad_greater(X,Ys).and the same, making use of first-argument indexing:
good_greater([],_X). good_greater([Y|Ys],X) :- X > Y, good_greater(Ys,X).
?- bad_greater(5,[1,2,3]).Expected answer:
yes
?- good_greater([1,2,3],5).Expected answer:
yesbad_greater/2 has the number first, so both clauses look the same to the indexer — the first argument is a variable in both of them — and a choice point is created at every step. good_greater/2 has the list first, so [] and [Y|Ys] can be told apart, and the system goes directly to the right clause.
test(N) :-
numbers(N, L),
X is N + 1,
speedup(bad_greater(X,L), good_greater(L,X)).%! \begin{focus}
numbers(0, []).
numbers(N, [N|Ns]) :-
N > 0,
N1 is N - 1,
numbers(N1, Ns).
%! \end{focus}
speedup(Slow, Fast) :-
speedup_loop(true, Slow, Fast).
speedup_loop(Loop, Slow, Fast) :-
time_of(Loop, Slow, TS),
time_of(Loop, Fast, TF),
show_speedup(TS, TF).
time_of(Loop, Goal, T) :-
repeat_time(1, Loop, Goal, T).
repeat_time(N, Loop, Goal, T) :-
loop_time(N, Goal, TG),
( TG >= 100
-> loop_time(N, Loop, TL),
( TG - TL > TG / 10 % (else too fast to tell
-> T is (TG - TL) / N % from the loop itself)
; T = 0
)
; N1 is N * 10,
repeat_time(N1, Loop, Goal, T)
).
loop_time(N, Goal, T) :-
statistics(runtime, [T0, _]),
( between(1, N, _), once(Goal), fail ; true ),
statistics(runtime, [T1, _]),
T is T1 - T0.
show_speedup(TS, TF) :-
format("Slow: ", []), show_time(TS),
format(", fast: ", []), show_time(TF),
( TS > 0, TF > 0
-> S is TS / TF,
format(", speedup: ~1f~n", [S])
; nl
).
show_time(T) :-
T > 0, !,
format("~4f ms", [T]).
show_time(_) :-
format("too fast to measure", []).
:- use_module(library(between)).?- test(600000).Expected answer (the times change from run to run):
Slow: 7.7279 ms, fast: 3.8335 ms, speedup: 2.0 yesWith 600,000 elements, good_greater/2 is about twice as fast, just for exchanging two arguments. Indexing can be used with structures other than lists, and it is available in most Prolog systems.
Both conditions are needed: the call must be the last one, and the predicate must be determinate at that point, which is what indexing helps with.
This is the recursive sum of the elements of a list:
:- module(_,_).
sum([], 0).
sum([N|Ns], Sum) :-
sum(Ns, Inter),
Sum is Inter + N.and this is the iterative version (tail recursive, using an accumulator):sum_iter(L, Res) :-
sum_acc(L, 0, Res).
sum_acc([], Res, Res).
sum_acc([N|Ns], In, Out) :-
Inter is In + N,
sum_acc(Ns, Inter, Out).?- sum([1,2,3],S).Expected answer:
S = 6 ?
?- sum_iter([1,2,3],S).Expected answer:
S = 6 ?sum/2 cannot be tail recursive: the addition has to wait for the recursive call to come back. The version with the accumulator does the addition first and then makes the recursive call, so nothing is pending when the call is made and the stack frame can be discarded.
test(N) :-
numbers(N, L),
speedup(sum(L,_), sum_iter(L,_)).%! \begin{focus}
numbers(0, []).
numbers(N, [N|Ns]) :-
N > 0,
N1 is N - 1,
numbers(N1, Ns).
%! \end{focus}
speedup(Slow, Fast) :-
speedup_loop(true, Slow, Fast).
speedup_loop(Loop, Slow, Fast) :-
time_of(Loop, Slow, TS),
time_of(Loop, Fast, TF),
show_speedup(TS, TF).
time_of(Loop, Goal, T) :-
repeat_time(1, Loop, Goal, T).
repeat_time(N, Loop, Goal, T) :-
loop_time(N, Goal, TG),
( TG >= 100
-> loop_time(N, Loop, TL),
( TG - TL > TG / 10 % (else too fast to tell
-> T is (TG - TL) / N % from the loop itself)
; T = 0
)
; N1 is N * 10,
repeat_time(N1, Loop, Goal, T)
).
loop_time(N, Goal, T) :-
statistics(runtime, [T0, _]),
( between(1, N, _), once(Goal), fail ; true ),
statistics(runtime, [T1, _]),
T is T1 - T0.
show_speedup(TS, TF) :-
format("Slow: ", []), show_time(TS),
format(", fast: ", []), show_time(TF),
( TS > 0, TF > 0
-> S is TS / TF,
format(", speedup: ~1f~n", [S])
; nl
).
show_time(T) :-
T > 0, !,
format("~4f ms", [T]).
show_time(_) :-
format("too fast to measure", []).
:- use_module(library(between)).?- test(100000).Expected answer (the times change from run to run):
Slow: 1.5548 ms, fast: 0.6411 ms, speedup: 2.4 yesWith 100,000 elements, sum_iter/2 is more than twice as fast as sum/2.
The basic skeleton to aim for is
<head> :-
<deterministic computation>,
<recursive call>.This is known as tail recursion, and it is a particular case of last call optimization. It also consumes less memory, which is often the more important half: for a list of two million elements, sum/2 needs some 16 MB of stack, and sum_iter/2 none.a :- test_1, !, ... a :- test_2, !, ... ... a :- test_n, !, ...If test_1 … test_n are mutually exclusive, the declarative meaning of the program is not affected: these are the white cuts, and they are free.
Otherwise, be careful. What is lost is not only correctness in some modes, but also declarativeness and readability: the ability to read a clause and know what it means without reading the ones above it.
As an example, take a predicate that goes through a list of numbers, replacing each negative number by its absolute value multiplied by two, and each of the others by itself multiplied by three. This version performs the arithmetic before the tests:
:- module(_,_).
x2x3([], []).
x2x3([X|Xs], [NX|NXs]) :-
NX is -X * 2,
X < 0,
x2x3(Xs, NXs).
x2x3([X|Xs], [NX|NXs]) :-
NX is X * 3,
X >= 0,
x2x3(Xs, NXs).and this one delays the arithmetic operations until after the tests:x2x3_1([], []).
x2x3_1([X|Xs], [NX|NXs]) :-
X < 0,
NX is -X * 2,
x2x3_1(Xs, NXs).
x2x3_1([X|Xs], [NX|NXs]) :-
X >= 0,
NX is X * 3,
x2x3_1(Xs, NXs).?- x2x3([1,-2,3],L).Expected answer:
L = [3,4,9] ?
?- x2x3_1([1,-2,3],L).Expected answer:
L = [3,4,9] ?The first version computes NX and then discovers that the clause does not apply, so the multiplication is wasted for every element that ends up in the other clause. The second one tests first. We compare them on a list of N numbers, half of them positive and half negative (N must be even):
test(N) :-
numbers(N, L),
speedup(x2x3(L,_), x2x3_1(L,_)).%! \begin{focus}
numbers(0, []).
numbers(N, [N,M|Ns]) :-
N > 0,
M is -N,
N1 is N - 2,
numbers(N1, Ns).
%! \end{focus}
speedup(Slow, Fast) :-
speedup_loop(true, Slow, Fast).
speedup_loop(Loop, Slow, Fast) :-
time_of(Loop, Slow, TS),
time_of(Loop, Fast, TF),
show_speedup(TS, TF).
time_of(Loop, Goal, T) :-
repeat_time(1, Loop, Goal, T).
repeat_time(N, Loop, Goal, T) :-
loop_time(N, Goal, TG),
( TG >= 100
-> loop_time(N, Loop, TL),
( TG - TL > TG / 10 % (else too fast to tell
-> T is (TG - TL) / N % from the loop itself)
; T = 0
)
; N1 is N * 10,
repeat_time(N1, Loop, Goal, T)
).
loop_time(N, Goal, T) :-
statistics(runtime, [T0, _]),
( between(1, N, _), once(Goal), fail ; true ),
statistics(runtime, [T1, _]),
T is T1 - T0.
show_speedup(TS, TF) :-
format("Slow: ", []), show_time(TS),
format(", fast: ", []), show_time(TF),
( TS > 0, TF > 0
-> S is TS / TF,
format(", speedup: ~1f~n", [S])
; nl
).
show_time(T) :-
T > 0, !,
format("~4f ms", [T]).
show_time(_) :-
format("too fast to measure", []).
:- use_module(library(between)).?- test(100000).Expected answer (the times change from run to run):
Slow: 3.9030 ms, fast: 3.2420 ms, speedup: 1.2 yesWith 100,000 elements, delaying the arithmetic is about 1.2 times faster.
We can go further, delaying the head unification and adding determinism. This is the first version again:
:- module(_,_).
x2x3([], []).
x2x3([X|Xs], [NX|NXs]) :-
NX is -X * 2,
X < 0,
x2x3(Xs, NXs).
x2x3([X|Xs], [NX|NXs]) :-
NX is X * 3,
X >= 0,
x2x3(Xs, NXs).and this is the new one:x2x3_2([], []).
x2x3_2([X|Xs], Out) :-
X < 0, !,
NX is -X * 2,
Out = [NX|NXs],
x2x3_2(Xs, NXs).
x2x3_2([X|Xs], Out) :-
X >= 0, !,
NX is X * 3,
Out = [NX|NXs],
x2x3_2(Xs, NXs).?- x2x3_2([1,-2,3],L).Expected answer:
L = [3,4,9] ?It takes the output cons cell out of the head, so that nothing is constructed until the clause has been committed to, and the cuts make the predicate determinate.
test(N) :-
numbers(N, L),
speedup(x2x3(L,_), x2x3_2(L,_)).%! \begin{focus}
numbers(0, []).
numbers(N, [N,M|Ns]) :-
N > 0,
M is -N,
N1 is N - 2,
numbers(N1, Ns).
%! \end{focus}
speedup(Slow, Fast) :-
speedup_loop(true, Slow, Fast).
speedup_loop(Loop, Slow, Fast) :-
time_of(Loop, Slow, TS),
time_of(Loop, Fast, TF),
show_speedup(TS, TF).
time_of(Loop, Goal, T) :-
repeat_time(1, Loop, Goal, T).
repeat_time(N, Loop, Goal, T) :-
loop_time(N, Goal, TG),
( TG >= 100
-> loop_time(N, Loop, TL),
( TG - TL > TG / 10 % (else too fast to tell
-> T is (TG - TL) / N % from the loop itself)
; T = 0
)
; N1 is N * 10,
repeat_time(N1, Loop, Goal, T)
).
loop_time(N, Goal, T) :-
statistics(runtime, [T0, _]),
( between(1, N, _), once(Goal), fail ; true ),
statistics(runtime, [T1, _]),
T is T1 - T0.
show_speedup(TS, TF) :-
format("Slow: ", []), show_time(TS),
format(", fast: ", []), show_time(TF),
( TS > 0, TF > 0
-> S is TS / TF,
format(", speedup: ~1f~n", [S])
; nl
).
show_time(T) :-
T > 0, !,
format("~4f ms", [T]).
show_time(_) :-
format("too fast to measure", []).
:- use_module(library(between)).?- test(100000).Expected answer (the times change from run to run):
Slow: 3.8941 ms, fast: 2.6997 ms, speedup: 1.4 yesIt is about 1.4 times faster than the first version, and it uses under a third of the memory (for a list of two million elements, some 70 MB instead of 230).