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 a04eb50
Showing 1 changed file with 32 additions and 5 deletions.
37 changes: 32 additions & 5 deletions src/lib/time.pl
Original file line number Diff line number Diff line change
Expand Up @@ -81,15 +81,42 @@

:- 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)
time_next_id(ID),
setup_call_cleanup(asserta(time_state(ID, T0)),
( call_cleanup(catch(Goal, E, (report_time(ID),throw(E))),
Det = true),
time_true(ID),
( Det == true -> !
; true
)
; report_time(ID),
false
),
report_time(T0)).
retractall(time_state(ID,_))).

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 a04eb50

Please sign in to comment.