Is it possible to get elapsed time in microseconds in Swi-Prolog? - time

Simple question.
I need to calculate how long it takes to execute predicate X. So I wrote this predicate:
chronometrise(X) :-
write('Executing: '), write(X), nl, nl,
statistics(walltime, _), call(X), statistics(walltime, [_,E]),
nl, write('Time: '), write(E), write(' ms.'), nl.
This allows me to get time in milliseconds. But I need microseconds.
Is it possible to calculate this in Swi-Prolog? Or milliseconds is best accuracy?
EDIT: Well, I found a better way: execute predicate 1000 times and calculate elapsed time. This will give us average execution time in milliseconds multiplyed by 1000, which is exactly average elapsed time in microseconds.

Instead of using statistics(walltime, _), use get_time(Time). This returns a wall time stamp as a float. The resolution of the reported time depends on the OS. The walltime statistics originates from Quintus compatibility.

Related

Why in the program "Prolog for eight puzzle" user slago uses the command: time(ids) I can't undestand why he uses this command

?- time(ids). What is the meaning of this command? What is the meaning of time?
I am expecting to undestand in Slago's 8-Puzzle proigram the menaing of command ?- time(ids). during execution
?- time(...). prints this at the end of slago's output:
% 97,719,612 inferences, 40.344 CPU in 40.991 seconds (98% CPU, 2422175 Lips)
It's the runtime of the solution in Prolog logical inferences and CPU use in seconds. Good for quick comparisons to see if changes to the code speed it up or slow it down, and good for posting here to show people roughly how long the code takes to search for a solution.

Prolog findall solutions within time limit

I would like to find all solutions to a goal within some time limit. I mean that I would like to search for solutions for at most the time limit, and return all solutions found, whether the time limit has been reached or not, which is regardless of whether the list of solutions is complete or not.
I have tried the following:
catch(call_with_time_limit(60, findall(S,my_goal(S),Sol)), time_limit_exceeded,false)
However, it does not return the partial list of solutions found if the time limit has been reached, and "false" can not be an expression relying on Sol.
This question is related to Prolog: "findall" for limited number of solutions, but instead of having a maximum number of solution I am interested in maximum search time.
Would you please have any insight for this?
Something like this may work to you:
findall_time(Duration, Template, Goal, Bag):-
get_time(TimeStamp),
Time is TimeStamp + Duration,
findall(Template, findall_time1(Time, Goal), Bag).
findall_time1(Time, Goal) :-
catch(setup_call_cleanup(alarm_at(Time,
throw(time_limit_exceeded),
Id),
(get_time(TimeStamp), Time>TimeStamp, Goal),
remove_alarm(Id)),
time_limit_exceeded,
fail).
with this sample code and test:
my_goal(X):-
between(0, 5, X),
sleep(1).
?- findall_time(2.5, X, my_goal(X), Bag).
Bag = [0, 1].

Reading in input using Prolog

Sorry if this is obvious, but I've been learning prolog recently and am attempting to read in a data to use in a recommender system.
gifter :- write('how much money? '), read(money), nl,
assert(will_spend(money)),
write('Is the giftee classy? '), read(classy), nl.
The previous code should read in the amount of money a user wishes to spend and then ask about the giftee's personality, however, only the first question is asked. It seems to get to the new line but not to asserting the predicate :
?- will_spend(30).
[WARNING: Undefined predicate: will_spend/1']
Why is this, what am I doing wrong? Thanks in advance for the help.
gifter :- write('how much money? '), read(Money), nl,
assert(will_spend(Money)),
write('Is the giftee classy? '), read(Classy), nl,
assert(classy :- Classy = 'yes').
Then,
?- gifter.
how much money? 127.
Is the giftee classy? yes.
true.
?- classy.
true.
?- will_spend(X).
X = 127.
Remember that read wants a period and a newline; also, variables should be capitalised.

PROLOG: Trying to get around predsort deleting duplicate objects

I'm trying to make it so predsort wont delete any duplicate objects that I have, and I think I know the logic in it but I can't seem to get it to work. I've been trying to call previous predicates that sort by another item when a duplicate is found but im getting errors when I try this.
process(3, X) :-
nl,
sort(X, Q),
show_records(Q),
nl, nl, menu(Q).
process(4, X) :-
nl,
predsort(sortName,X,Q),
show_records(Q),
nl, nl, menu(Q).
sortName(T, [_,E,_], [_,R,_]) :-
(compare(T, E, R)
->process(3, X)
).
process(5, X) :-
nl,
predsort(sortGrade,X,Q),
show_records(Q),
nl, nl, menu(Q).
sortGrade(T, [_,_,E], [_,_,R]) :-
(compare(T, E, R)
->process(4,X)
).
Process 3 sorts by the first value in the sublist, 4 the second one, and 5 the last. I'm trying to make it where when 5 finds a duplicate it'll go to 4, and from 4 to three if need be.
I will answer anyway, even if you spotted you're own solution, since it's not clear to me if ditto solution matches with mine.
What I mean: time ago I wrote my own predmsort/3 (predating in obvious mode the terminoloy of sort/msort) for exactly the same reason you have reported.
Time passed before I realized that there was a much better solution to my problem:
just never return =.
That is, so simple...

built-in predicate statistics in a Bratko example doesn't behave as expected

I'm trying out exercise 4.3 of Bratko with the addition of statistics. It seems that the moment I add statistics, I'm getting a 'no' answer from Prolog or in some cases it ends up in an endless loop...
For instance, this questions gives me a clear answer of the Schedule:
?- schedule(riva at Start, riva at End, Schedule),
member(arrive(malcesine at _), Schedule),
before(17:00, Start).
But if I add statistics like this:
?- statistics(runtime, T1),
schedule(riva at Start, riva at End, Schedule),
member(arrive(malcesine at _), Schedule),
before(17:00, Start),
statistics(runtime, T2).
Prolog answers 'no'.
Could the reason be that I'm using Amzi Prolog?
The predicate statistics/2 is not a standard predicate (although found in some Prolog implementations). What do you get if you try the goal:
?- predicate_property(statistics(_, _), built_in).
Or:
?- predicate_property(statistics(_, _), imported_from(amzi_system))
If both goals fail, you may want to look at any libraries provided by your Prolog system to check if similar functionality is available.

Resources