Works also with random data, single element insert via union/3
is faster than via ord_union/3. No C code involved, was using union4/3:
?- findall(X,(between(1,10000,_),random(1,10000,X)),L),
time((between(1,10000,_),
random(1,10000,Y), union4([Y],L,_), fail; true)).
% 63,128,346 inferences, 3.250 CPU in 3.287 seconds (99% CPU, 19424106 Lips)
?- findall(X,(between(1,10000,_),random(1,10000,X)),L), sort(L,R),
time((between(1,10000,_),
random(1,10000,Y), ord_union([Y],R,_), fail; true)).
% 96,026,708 inferences, 5.516 CPU in 5.564 seconds (99% CPU, 17409941 Lips)
Edit 16.08.2022
The library ordset pays off when you insert like 5 elements in a batch:
?- findall(X,(between(1,10000,_),random(1,10000,X)),L),
time((between(1,10000,_),
findall(Y,(between(1,5,_), random(1,10000,Y)), H),
union4(H,L,_), fail; true)).
% 315,183,160 inferences, 15.984 CPU in 15.991 seconds (100% CPU, 19718204 Lips)
?- findall(X,(between(1,10000,_),random(1,10000,X)),L),sort(L,R),
time((between(1,10000,_),
findall(Y,(between(1,5,_), random(1,10000,Y)), H),sort(H,J),
ord_union(J,R,_), fail; true)).
% 157,835,640 inferences, 7.812 CPU in 7.814 seconds (100% CPU, 20202962 Lips)