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 fda1482045 Logtalk 2.18.0 release files.
git-svn-id: https://yap.svn.sf.net/svnroot/yap/trunk@1090 b08c6af1-5177-4d33-ba66-4b1c6b8b522a
2004-07-08 23:48:59 +00:00

55 lines
1.0 KiB
Plaintext

:- object(timer).
:- info([
version is 1.1,
author is 'Paulo Moura',
date is 2004/6/30,
comment is 'Call executing time profiler.']).
:- uses(time).
:- uses(loop).
:- 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) :-
time::cpu_time(Start),
(call(Call) -> true; true),
time::cpu_time(End),
Time is End - Start.
timer(Call, Times, Time) :-
time::cpu_time(Start),
loop::forto(1, Times, Call),
time::cpu_time(End),
time::cpu_time(Start2),
loop::forto(1, 0, true),
time::cpu_time(End2),
Overhead is End2 - Start2,
Time is (End - Start - Overhead) / Times.
:- end_object.