显示谓词子句每个目标的执行时间 [英] Display the execution times for each goal of a predicate clause
问题描述
我想使用 SICStus Prolog 查看谓词目标内的执行时间.
I want to see the execution time inside goals of predicate with SICStus Prolog.
示例:
pred :-
goal1,
time,
goal2,
time.
go :-
call(pred).
time_go :-
go,
times(go).
预期结果:
?- time_go.
times_go = 1000ms ,
times_go_goal1 = 500ms,
times_go_goal2 = 500ms
怎么做?
我尝试了 library(timeout)
中的 time_out(:Goal, +Time, -Result)
但我收到了这个错误:
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
推荐答案
您可以使用 statistics/2
:
statistics(runtime,[Start|_]),
do_something,
statistics(runtime,[Stop|_]),
Runtime is Stop - Start.
或者,如果您想包括垃圾收集时间,您可以使用 total_runtime
而不是 runtime
.Start
和 Stop
以毫秒为单位,但上次我将它与 SICStus 一起使用时,它只返回 10 的倍数.在一个项目中,我们使用了对自定义外部 C 库的调用检索更精细的分辨率.
Alternatively, you can use total_runtime
instead of runtime
if you want to include time for garbage collection. Start
and Stop
are measured in milliseconds, but the last time I used it with SICStus it returned only multiple of 10. In a project we used calls to a custom external C library to retrieve a finer resolution.
对time_out/3
的说明:它用于限制目标的运行时间,而不是测量其运行时间.如果目标及时完成,结果为success
,如果需要更多时间则中止执行(内部抛出超时异常),结果为timeout
.
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 is success
, if it needs more time the execution is aborted (internally a timeout exception is thrown) and the result is timeout
.
这篇关于显示谓词子句每个目标的执行时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!