Tabling for memoizing - performance

To speed up some mathematical calculations I thought about using simple tabled memoizing, but the tabled version is slower; anything I am missing? Or is the overhead of tabling much greater than calculating atan2 every time?

Here is the test code:

run :-
   N=2_000_000,
   time(forall(between(1,N,N1),tabled_atan2(N1,N1,_))),
   time(forall(between(1,N,N1),regular_atan2(N1,N1,_))).

regular_atan2(X,Y,Out) :-
   Out is atan2(Y,X).

:- table tabled_atan2/3.
tabled_atan2(X,Y,Out) :-
   Out is atan2(Y,X).

The results:

5 ?- run.
% 68,000,012 inferences, 18.263 CPU in 18.285 seconds (100% CPU, 3723285 Lips)
% 5,999,999 inferences, 0.464 CPU in 0.464 seconds (100% CPU, 12938835 Lips)
true.

6 ?- run.
% 9,999,999 inferences, 1.913 CPU in 1.915 seconds (100% CPU, 5227350 Lips)
% 5,999,999 inferences, 0.463 CPU in 0.463 seconds (100% CPU, 12953854 Lips)
true.

So the tabling version (after the table is created) is about 4 times slower than calculating it each time. Is there some glaring error in my code? This is with 8.1.24.

No really unexpected :slight_smile: The current overhead of tabled goals in SWI-Prolog is rather high and wlll go down (probably), but it is unlikely that it will ever win from the hardware float instruction. Tabling only wastes memory for simple non-recursive predicates. It gets interesting to break non-terminating recursion (that is logically correct) or where it avoids redoing more expensive computations.

I figured it would be the tabling overhead, but I didn’t expect it to be 4 times higher. Thanks for the great work in tabling though, it greatly advances swi-prolog.