Created
September 28, 2019 12:18
-
-
Save JanWielemaker/420e9afa9f791e636dbf6c41239b5c1f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
% This program reproduces a performance problem with incremental tabling. | |
:- import incr_asserta/1 from increval. | |
%:- dynamic facts/1 as incremental,abstract(0). | |
:- dynamic facts/1 as incremental. | |
add_facts(N) :- | |
randset(N, 10 000 000, S), | |
forall(member(X, S), incr_asserta(facts(X))). | |
:- table is_adjacent/1 as incremental. | |
is_adjacent(X) :- | |
facts(X), | |
(Y is X+1; Y is X-1), | |
facts(Y). | |
count_adjacent :- | |
setof(X, is_adjacent(X), L), | |
length(L, Len), | |
write('There are '), write(Len), writeln(' adjacent numbers'). | |
add_and_count(N) :- | |
write('Adding '), write(N), writeln(' facts...'), | |
time(add_facts(N)), | |
writeln('Counting'), | |
time(count_adjacent). | |
go_tabled :- | |
forall(member(X, [100 000, 1, 10, 100, 1000, 10000, 100000]), | |
add_and_count(X)). | |
/******************************* | |
* NON-TABLED * | |
*******************************/ | |
:- dynamic fact_nt/1. | |
add_facts_2(N) :- | |
randset(N, 10 000 000, S), | |
forall(member(X, S), asserta(fact_nt(X))). | |
count_adjacent_2 :- | |
setof(X, fact_nt(X), Xs), | |
aggregate_all(count, adjacent(Xs, X), Count), | |
write('There are '), write(Count), writeln(' adjacent numbers'). | |
adjacent(Xs, X) :- | |
append(_,[X,Y|_], Xs), | |
Y =:= X+1. | |
add_and_count_2(N) :- | |
write('Adding '), write(N), writeln(' facts...'), | |
time(add_facts_2(N)), | |
writeln('Counting'), | |
time(count_adjacent_2). | |
go_non_tabled :- | |
forall(member(X, [100 000, 1, 10, 100, 1000, 10000, 100000]), | |
add_and_count_2(X)). | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment