Advertisement
Guest User

Untitled

a guest
Nov 15th, 2024
18
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.97 KB | Source Code | 0 0
  1. :- use_module(library(clpfd)).
  2. :- use_module(library(lists)).
  3. :- use_module(library(time)).
  4.  
  5. elem_at(Index, List, Elem) :-
  6. elem_at_inner(Index, 0, List, Elem).
  7. elem_at_inner(Index, Index, [Elem | _], Elem).
  8. elem_at_inner(Index, IndexCandidate, [_ | Rest], Elem) :-
  9. Index #> IndexCandidate,
  10. NewIndexCandidate #= IndexCandidate + 1,
  11. elem_at_inner(Index, NewIndexCandidate, Rest, Elem).
  12.  
  13. lookup1(List, N, M, Elem) :-
  14. nth0(Index, List, Elem),
  15. N =< Index, Index < M.
  16.  
  17. lookup2(List, N, M, Elem) :-
  18. between(N, M, Index),
  19. nth0(Index, List, Elem).
  20.  
  21. lookup3(List, N, M, Elem) :-
  22. N #=< Index, Index #< M,
  23. elem_at(Index, List, Elem).
  24.  
  25. testall :-
  26. length(List, 10 000),
  27. time(findall(E, lookup1(List, 0, 5000, E), _)), % 'naive'),
  28. time(findall(E, lookup2(List, 0, 5000, E), _)), % 'between'),
  29. time(findall(E, lookup3(List, 0, 5000, E), _)). % 'constraint').
  30.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement