Skip to content

Commit

Permalink
ENHANCED: Better reporting for time/1.
Browse files Browse the repository at this point in the history
This addresses mthom#378.
  • Loading branch information
triska committed Nov 30, 2021
1 parent 7abdade commit bdf53bd
Showing 1 changed file with 34 additions and 6 deletions.
40 changes: 34 additions & 6 deletions src/lib/time.pl
Original file line number Diff line number Diff line change
Expand Up @@ -81,15 +81,43 @@

:- meta_predicate time(0).

:- dynamic(time_id/1).
:- dynamic(time_state/2).

time_next_id(N) :-
( time_id(N0) ->
N is N0 + 1
; N = 0
),
asserta(time_id(N)).

time(Goal) :-
'$cpu_now'(T0),
setup_call_cleanup(true,
( Goal,
report_time(T0)
),
report_time(T0)).
time_next_id(ID),
asserta(time_state(ID, T0)),
( call_cleanup(catch(Goal, E, (report_time(ID),throw(E))),
( Det = true,
retractall(time_state(ID,_))
)),
time_true(ID),
( Det == true -> !
; true
)
; report_time(ID),
false
).

time_true(ID) :-
report_time(ID).
time_true(ID) :-
% on backtracking, update the stored walltime for this ID
retract(time_state(ID, _)),
'$cpu_now'(T0),
asserta(time_state(ID, T0)),
false.

report_time(T0) :-
report_time(ID) :-
time_state(ID, T0),
'$cpu_now'(T),
Time is T - T0,
( bb_get('$first_answer', true) ->
Expand Down

0 comments on commit bdf53bd

Please sign in to comment.