How can I keep up via higher order programming (Haskell style)
with the performance of hand written loops (pre call/n Prolog)?
I tried these tests:
sum([], X, X).
sum([H|T], X, Y) :- plus(X,H,Z), sum(T, Z, Y).
test :- time((between(1,100000,_),
sum([1,2,3,4,5,6,7,8,9], 0, _), fail; true)).
test2 :- time((between(1,100000,_),
foldl(plus, [1,2,3,4,5,6,7,8,9], 0, N), fail; true)).
Even when I have library(apply_macros)
loaded,
the hand written loop is still faster:
?- test.
% 2,000,001 inferences, 0.063 CPU in 0.076 seconds
true.
?- test2.
% 2,100,141 inferences, 0.125 CPU in 0.122 seconds
true.
Actually test
is twice as fast as test2
!