I want to see the execution time inside goals of predicate with SICStus Prolog.
Example :
pred :-
goal1,
time,
goal2,
time.
go :-
call(pred).
time_go :-
go,
times(go).
Expected result:
?- time_go.
times_go = 1000ms ,
times_go_goal1 = 500ms,
times_go_goal2 = 500ms
How to do that ?
I tried time_out(:Goal, +Time, -Result)
from library(timeout)
but I got this error:
| ?- time_out(char_code(a,N), T, Res).
! Instantiation error in argument 2 of user:time_out/3
! goal: time_out(user:char_code(a,_193),_179,_181)
| ?- time_out(char_code(a,N), 1000, Res).
N = 97,
Res = success ? ; % Res=timeout in other example
Since nobody mentioned it, if you only want a time display the time/1 predicate can be also useful. It is already supported by a couple of Prolog systems such as SWI-Prolog, Jekejeke Prolog, O-Prolog, etc...:
Unfortunately Prolog systems such as GNU Prolog, SICStus Prolog dont support it. But its the analog of the Unix time command, in that it is a meta predicate that takes a goal argument. Better than stone age methods.
You can use
statistics/2
for that:Alternatively, you can use
total_runtime
instead ofruntime
if you want to include time for garbage collection. I cannot remember the unit which is used forStart
andStop
, but it is quite coarse IIRC. In a project we used calls to a custom external C library to retrieve a finer resolution.A remark to
time_out/3
: It is used to limit the runtime of a goal, not to measure its runtime. If the goal finishes in time, the result issuccess
, if it needs more time the execution is aborted (internally a timeout exception is thrown) and the result istimeout
.I'd like to add two thoughts:
Prolog allows for backtracking, so goals may succeed more than once.
What's the "runtime" you are interested in?
Use
statistics/2
, but don't do it directly. Instead, use an abstraction likecall_time/2
.Sample query:
Notice that
call_time/2
succeeds twice andT_ms
measures total runtime up to this point.