☰

Efficiency Issues in Prolog

Course Notes


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!

  • Programs are loaded when the page opens: a loaded program has a check mark (✔) in its top right corner. If you edit a program, the mark turns into a right-pointing triangle (▶): press it to load your version.
  • Queries are the boxes that start with ?-. Press the ▶ of a query box to run it against the programs above it. While an answer ends in ? there may be more: press Next (or type ; and Enter) for another one, or Stop.
  • Expected answers are printed in plain text underneath each query box, so you can read the notes straight through without running anything.
  • Overall, the northeast pointing arrow (↗) of a program cell opens it in a separate Prolog playground window.

What efficiency means here

Efficiency (Slide 2)

Efficiency means savings, and not only of time (the number of unifications, of reduction steps, the logical inferences per second, and so on) but also of memory. In a language that allocates as freely as this one, memory is often the binding constraint.

The general advice is the general advice of every language:

  • use the best algorithms;
  • use the appropriate data structures.
And one warning that governs all that follows: each programming paradigm has its own techniques. They should not be adopted blindly from elsewhere. Several of the "obvious" things to do in Prolog are obvious only to someone who learned them in another language, and they are the expensive ones.

A note on the timings. Absolute times obviously depend on the system (the computer, the Prolog implementation, and so on), so the relevant measure is the speedup that is observed, and that is what the examples below show. Each example comes with a query that measures it, which can be run in this page. The outputs printed here were obtained running the programs directly in Ciao 1.26 on a laptop; when running Ciao in the browser the times are larger, because of the compilation to wasm, and the speedups can also differ somewhat.

Data structures

Data structures (Slides 3–4)

Do not make excessive use of lists. A list is the right structure when the number of elements is not known in advance. (And then it is sometimes worth keeping it ordered: set equality, for example, becomes a unification, as we will see below.)

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".

Let unification do the work

Swapping two arguments (Slide 5)

Unification is very powerful. Use it!

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

yes
The short version is about three times faster.

How the speedups are measured. These are the auxiliary predicates that all the examples in this part use. speedup(Slow, Fast) shows the time, in milliseconds, of one run of each goal, and how many times faster Fast is. Each goal is repeated until it has run for at least 100 ms, and the time of the loop itself is subtracted, so that short goals can also be measured (when the time of a goal cannot be told apart from that of the loop, it is reported as "too fast to measure"). speedup_loop(Loop, Slow, Fast) also subtracts the time of Loop, the empty loop around the calls that are being compared.

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.

Checking that a list has three elements (Slide 6)

The same lesson, in a form that is easier to fall into. This is a weak answer, which always traverses the list and computes its length:

:- 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:

no
The 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

The dynamic database

Do not simulate global variables (Slide 7)

The database is not a place to keep counters. This program uses it to simulate a global variable:

:- 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

yes
good_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.

Where the database is the right answer: lemmas (Slide 8)

There is a legitimate use, which we have already seen: asserting results which have been found to be true, that is, lemmas. The Fibonacci sequence, computed directly from its definition, recomputes the same values an exponential number of times:

:- 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

yes
The 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

yes
And 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

yes
lfib(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.

Warning: this is only useful when intermediate results are reused. If every call asks a different question, we pay the cost of the assertions and get nothing back — and we pay in memory for every lemma that is stored, for as long as the program runs.

Determinism

Do not keep alternatives that are not needed (Slide 9)

Many problems are deterministic. Non-determinism is useful — it gives us automatic search — but it is expensive: every choice point that is left behind is memory that cannot be reclaimed, and a branch to which the system must be prepared to return.

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

no
Program 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 ? ;

no
Both 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

yes
The speedup grows with N, as it must when one version is quadratic and the other one linear.

Choosing the algorithm (Slide 10)

Now let us check that two (ground) lists contain the same elements. The naive version says exactly what the problem says, that no element of either list is missing from the other one:

:- 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:

yes
The 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

yes
With 1000 elements, sorting and unifying is several hundred times faster, and the speedup grows with the size of the lists.

Iteration, cuts and delaying

Iteration versus recursion (Slides 14–15)

When the recursive call is the last subgoal in the clause and there are no alternatives left in the execution of the predicate, we have an iteration rather than a recursion. It is much more efficient, in time and in memory.

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

yes
With 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.

Cuts (Slide 16)

Cuts eliminate choice points, so they "create" determinism:

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.

Delaying work (Slides 17–18)

The principle: do not perform useless operations. In general, do not do anything until it is necessary, and put the tests as soon as possible.

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

yes
With 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

yes
It 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).

A piece of advice. These techniques are for when performance is essential. They make programs harder to understand, harder to debug and harder to maintain — the last version above is noticeably less readable than the first one. Measure first, and optimize the part that actually costs.

Conclusions

Conclusions (Slide 19)

  • Avoid inheriting programming styles from other languages. Most of the expensive patterns that we have seen are habits from elsewhere.
  • Program in a declarative way. It improves readability, and it allows compiler optimizations: a declarative program gives the system more to work with, not less.
  • Avoid using the dynamic database when possible. Lemmas are the exception, not the rule.
  • Look for deterministic computations when programming deterministic problems.
  • Put tests as soon as possible in the program, so that the tree is pruned early.
  • Delay computations until they are needed.
A final thought: learning Prolog implementation techniques — the Warren Abstract Machine in particular — is very instructive and useful, and it makes most of the advice above obvious rather than memorized. There are slides and a book on the topic.