This repository has been archived on 2023-08-20. You can view files and clone it, but cannot push or open issues or pull requests.
yap-6.3/Logtalk/examples/profiling/timer.lgt

55 lines
1008 B
Plaintext
Raw Normal View History

:- object(timer).
:- info([
version is 1.3,
author is 'Paulo Moura',
date is 2006/9/17,
comment is 'Call executing time profiler.']).
:- uses(time, [cpu_time/1]).
:- uses(loop, [forto/3]).
:- public(timer/2).
:- meta_predicate(timer(::, *)).
:- mode(timer(+callable, -number), one).
:- info(timer/2,
[comment is 'Returns time to execute a call.',
argnames is ['Call', 'Time']]).
:- public(timer/3).
:- meta_predicate(timer(::, *, *)).
:- mode(timer(+callable, +integer, -float), one).
:- info(timer/3,
[comment is 'Returns the average time needed to to execute a call.',
argnames is ['Call', 'Times', 'Time']]).
timer(Call, Time) :-
cpu_time(Start),
(call(Call) -> true; true),
cpu_time(End),
Time is End - Start.
timer(Call, Times, Time) :-
cpu_time(Start),
forto(1, Times, Call),
cpu_time(End),
cpu_time(Start2),
forto(1, 0, true),
cpu_time(End2),
Overhead is End2 - Start2,
Time is (End - Start - Overhead) / Times.
:- end_object.