]> Repositorios git - scryer-prolog.git/commitdiff
ENHANCED: time(Goal) now also reports the time if Goal fails
authorMarkus Triska <[email protected]>
Sun, 19 Apr 2020 19:13:15 +0000 (21:13 +0200)
committerMarkus Triska <[email protected]>
Sun, 19 Apr 2020 19:13:15 +0000 (21:13 +0200)
src/prolog/lib/time.pl

index 2713ebfeaab38a0d1d057c2a1590a9848e95ed84..97358a7d8cbbce9862b638a4d18bdda43fd0d218 100644 (file)
 
 time(Goal) :-
         '$cpu_now'(T0),
-        Goal,
+        setup_call_cleanup(true,
+                           (   Goal,
+                               report_time(T0)
+                           ),
+                           report_time(T0)).
+
+report_time(T0) :-
         '$cpu_now'(T),
         Time is T - T0,
         (   bb_get('$first_answer', true) ->
@@ -28,7 +34,8 @@ time(Goal) :-
 ?- time((true;false)).
    % CPU time: 0.000 seconds
    true
-;  false.
+;  % CPU time: 0.001 seconds
+   false.
 
 :- time(use_module(library(clpz))).
    % CPU time: 2.762 seconds
@@ -38,7 +45,8 @@ time(Goal) :-
 :- time(use_module(library(lists))).
    % CPU time: 0.000 seconds
    true
-;  false.
+;  % CPU time: 0.001 seconds
+   false.
 
 ?- time(member(X, [a,b,c])).
    % CPU time: 0.000 seconds
@@ -47,5 +55,6 @@ time(Goal) :-
    X = b
 ;  % CPU time: 0.004 seconds
    X = c
-;  false.
+;  % CPU time: 0.007 seconds
+   false.
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */