SWI Performace - removing single unique fact with retract vs. retractall?

SWI Performace - removing single unique fact with retract vs. retractall?

Post by robert szy » Mon, 27 Sep 2010 07:06:41


C++ application with embedded SWI Prolog. The prolog database consists
of many unique facts with arrity 1. It does a lot of assert and
retract calls.Normally I shouldn need to call retractall because all
terms are unique so if everything runs smoothly just calling retract
should be fine. However to guard against some unexpected conditions it
would be safer to call retractall just in case. The question is what
would be performance hit if I replace retract with retractall? Keep in
mind I will be almost always removing just one fact with retractall
call.
 
 
 

SWI Performace - removing single unique fact with retract vs. retractall?

Post by pjb » Mon, 27 Sep 2010 07:45:48

robert szymanski < XXXX@XXXXX.COM > writes:


I would expect retractall to be faster than retract.

--
__Pascal Bourguignon__ http://www.yqcomputer.com/

 
 
 

SWI Performace - removing single unique fact with retract vs. retractall?

Post by Jan Wielem » Mon, 27 Sep 2010 17:45:42


Why? They've got to do more or less the same: find the clause and
delete it. Now, retract will check the index and determine determinsm
and retractall actually searches for the next clause on the same index.

But, as always, why not simply test it:

c :-
forall(between(1, 1000000, X),
assert(a(X))).

r1 :-
( between(1, 100000, _),
A is random(1000000),
retractall(a(A)),
fail
; true
).


r2 :-
( between(1, 100000, _),
A is random(1000000),
retract(a(A)),
fail
; true
).


Starting fresh, run c and either r1 or r2 gives:

2 ?- time(r1).
% 200,003 inferences, 0.540 CPU in 0.540 seconds (100% CPU, 370178 Lips)
true.

2 ?- time(r2).
% 200,003 inferences, 0.562 CPU in 0.581 seconds (97% CPU, 356162 Lips)
true.

The 8% can hardly be called significant. (AMD 5400+)

--- Jan
 
 
 

SWI Performace - removing single unique fact with retract vs. retractall?

Post by bart demoe » Wed, 29 Sep 2010 20:47:57


For another test:

t1 :- retractall(a(_)), fail.
t1.

t2 :- retract(a(_)), fail.
t2.

?- time(t1).
% 3 inferences, 0.227 CPU in 0.227 seconds (100% CPU, 13 Lips)


?- time(t2).
% 3 inferences, 0.688 CPU in 0.688 seconds (100% CPU, 4 Lips)

the difference is significant.

Cheers

Bart Demoen
 
 
 

SWI Performace - removing single unique fact with retract vs. retractall?

Post by Jan Wielem » Thu, 30 Sep 2010 01:02:45


Yip. retractall/1 has to do the job of finding the predicate, locking
it, etc. only once. I think you are running an old version because the
time difference here is a factor 4. Recent versions also verify that
all arguments are unbound and then simply delete all clauses. That
is afterall a pretty common pattern. retract must decompile because
it must bind the argument.

Cheers --- Jan
 
 
 

SWI Performace - removing single unique fact with retract vs. retractall?

Post by Jan Burs » Thu, 30 Sep 2010 01:59:42

bart demoen schrieb:

So counting inferences is not a good measure to count
the steps the interpreter has to execute. Since the
retract(a(_)), fail loop totally disappears.

Bye
 
 
 

SWI Performace - removing single unique fact with retract vs. retractall?

Post by Jan Wielem » Thu, 30 Sep 2010 04:19:56


Nope, but apparantly the inference count isn't updated everywhere
as it should be. Similar things happen to the profiler; at some
points hooks are not called where they should be.

--- Jan