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
pmoura f5e660b9a3 Logtalk 2.20.0 files.
git-svn-id: https://yap.svn.sf.net/svnroot/yap/trunk@1114 b08c6af1-5177-4d33-ba66-4b1c6b8b522a
2004-08-16 16:41:11 +00:00

55 lines
1006 B
Plaintext

:- object(timer).
:- info([
version is 1.2,
author is 'Paulo Moura',
date is 2004/8/15,
comment is 'Call executing time profiler.']).
:- uses(time, [cpu_time/1]).
:- uses(loop, [forto/3]).
:- public(timer/2).
:- metapredicate(timer(::, *)).
:- mode(timer(+callable, -number), one).
:- info(timer/2,
[comment is 'Returns time to execute a call.',
argnames is ['Call', 'Time']]).
:- public(timer/3).
:- metapredicate(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.