forked from mthom/scryer-prolog
-
Notifications
You must be signed in to change notification settings - Fork 0
/
time.pl
209 lines (177 loc) · 6.28 KB
/
time.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Written 2020-2024 by Markus Triska ([email protected])
Part of Scryer Prolog.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
/** This library provides predicates for reasoning about time.
*/
:- module(time, [max_sleep_time/1,
sleep/1,
time/1,
current_time/1,
format_time//2,
statistics/2
]).
:- use_module(library(format)).
:- use_module(library(iso_ext)).
:- use_module(library(error)).
:- use_module(library(dcgs)).
:- use_module(library(lists)).
:- use_module(library(charsio), [read_from_chars/2]).
%% current_time(-T)
%
% Yields the current system time _T_ in an opaque form, called a
% _time stamp_. Use `format_time//2` to describe strings that contain
% attributes of the time stamp.
current_time(T) :-
'$current_time'(T0),
read_from_chars(T0, T).
%% format_time(FormatString, TimeStamp)//
%
% The nonterminal format_time//2 describes a list of characters that
% are formatted according to a format string. Usage:
%
% ```
% phrase(format_time(FormatString, TimeStamp), Cs)
% ```
%
% TimeStamp represents a moment in time in an opaque form, as for
% example obtained by `current_time/1`.
%
% FormatString is a list of characters that are interpreted literally,
% except for the following specifiers (and possibly more in the future):
%
% | `%Y` | year of the time stamp. Example: 2020. |
% | `%m` | month number (01-12), zero-padded to 2 digits |
% | `%d` | day number (01-31), zero-padded to 2 digits |
% | `%H` | hour number (00-24), zero-padded to 2 digits |
% | `%M` | minute number (00-59), zero-padded to 2 digits |
% | `%S` | second number (00-60), zero-padded to 2 digits |
% | `%b` | abbreviated month name, always 3 letters |
% | `%a` | abbreviated weekday name, always 3 letters |
% | `%A` | full weekday name |
% | `%j` | day of the year (001-366), zero-padded to 3 digits |
% | `%%` | the literal `%` |
%
% Example:
%
% ```
% ?- current_time(T), phrase(format_time("%d.%m.%Y (%H:%M:%S)", T), Cs).
% T = [...], Cs = "11.06.2020 (00:24:32)".
% ```
format_time([], _) --> [].
format_time(['%','%'|Fs], T) --> !, "%", format_time(Fs, T).
format_time(['%',Spec|Fs], T) --> !,
( { member(Spec=Value, T) } ->
seq(Value)
; { domain_error(time_specifier, Spec, format_time//2) }
),
format_time(Fs, T).
format_time([F|Fs], T) --> [F], format_time(Fs, T).
%% max_sleep_time(T)
%
% The maximum admissible time span for `sleep/1`.
max_sleep_time(0xfffffffffffffbff).
%% sleep(S)
%
% Sleeps for S seconds (a floating point number or integer).
sleep(T) :-
builtins:must_be_number(T, sleep),
( T < 0 ->
domain_error(not_less_than_zero, T, sleep/1)
; max_sleep_time(N), T > N ->
throw(error(representation_error(max_sleep_time), sleep/1))
; '$sleep'(T)
).
%% statistics(?Keyword, ?List)
%
% Preliminary support for statistics/2, yielding timing information.
% The only supported `Keyword` is `runtime`. The first element of
% `List` is the CPU time in milliseconds, the second element is
% currently not supported.
statistics(runtime, [T,unsupported]) :-
'$cpu_now'(T0),
T is T0*1000.
:- meta_predicate time(0).
:- dynamic(time_id/1).
:- dynamic(time_state/3).
time_next_id(N) :-
( retract(time_id(N0)) ->
N is N0 + 1
; N = 0
),
asserta(time_id(N)).
%% time(Goal)
%
% Reports the execution time of Goal.
time(Goal) :-
cputime_inferences(T0, I0),
time_next_id(ID),
setup_call_cleanup(asserta(time_state(ID, T0, I0)),
( call_cleanup(catch(Goal, E, (report_time(ID),throw(E))),
Det = true),
time_true(ID),
( Det == true -> !
; true
)
; report_time(ID),
false
),
retract(time_state(ID, _, _))).
cputime_inferences(T, I) :-
'$cpu_now'(T),
'$inference_count'(I).
time_true(ID) :-
report_time(ID).
time_true(ID) :-
% on backtracking, update the stored CPU time for this ID
retract(time_state(ID, _, _)),
cputime_inferences(T0, I0),
asserta(time_state(ID, T0, I0)),
false.
report_time(ID) :-
time_state(ID, T0, I0),
cputime_inferences(T, I),
Time is T - T0,
Inferences0 is I - I0,
% we must subtract the number of inferences that time/1 itself takes;
% this may have to be adapted if the implementation changes,
% so that (for example) true/1 takes exactly 1 inference.
( bb_get('$answer_count', 0) ->
Inferences is Inferences0 - 60,
Pre = " ", Post = ""
; Inferences is Inferences0 - 9,
Pre = "", Post = " "
),
phrase((Pre,"% CPU time: ", format_("~3f", [Time]), "s, ",
format_("~U", [Inferences])," inference",s_if_necessary(Inferences),"\n",
Post), Cs),
format("~s", [Cs]).
s_if_necessary(Inferences) -->
{ compare(C, 1, Inferences) },
s_(C).
s_(=) --> "".
s_(<) --> "s".
s_(>) --> " (exception?)".
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
?- time((true;false)).
% CPU time: 0.000s, 1 inference
true
; % CPU time: 0.000s, 0 inference (exception?)
false.
:- time(use_module(library(clpz))).
% CPU time: 0.343s, 409_874 inferences
true.
:- time(use_module(library(lists))).
% CPU time: 0.000s, 19 inferences
true.
?- time(member(X, "abc")).
% CPU time: 0.000s, 1 inference
X = a
; % CPU time: 0.000s, 3 inferences
X = b
; % CPU time: 0.000s, 3 inferences
X = c.
?- time((repeat,false)).
% CPU time: 2.726s, 53_330_502 inferences
error('$interrupt_thrown',repl/0).
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */