ADDED: library(time), providing time/1 for benchmarking

sleep/1 and predicates for reasoning about time stamps should also
be added to this library.
This commit is contained in:
Markus Triska
2020-04-19 19:13:27 +02:00
parent cae0733149
commit 4693c23a49
5 changed files with 77 additions and 1 deletions

View File

@@ -241,6 +241,7 @@ pub enum SystemClauseType {
GetDoubleQuotes,
InstallNewBlock,
Maybe,
CPU_now,
QuotedToken,
ReadTermFromChars,
ResetBlock,
@@ -347,6 +348,7 @@ impl SystemClauseType {
&SystemClauseType::PartialStringTail => clause_name!("$partial_string_tail"),
&SystemClauseType::LiftedHeapLength => clause_name!("$lh_length"),
&SystemClauseType::Maybe => clause_name!("maybe"),
&SystemClauseType::CPU_now => clause_name!("$cpu_now"),
&SystemClauseType::ModuleAssertDynamicPredicateToFront => {
clause_name!("$module_asserta")
}
@@ -480,6 +482,7 @@ impl SystemClauseType {
("$install_inference_counter", 3) => Some(SystemClauseType::InstallInferenceCounter),
("$lh_length", 1) => Some(SystemClauseType::LiftedHeapLength),
("$maybe", 0) => Some(SystemClauseType::Maybe),
("$cpu_now", 1) => Some(SystemClauseType::CPU_now),
("$module_exists", 1) => Some(SystemClauseType::ModuleExists),
("$module_of", 2) => Some(SystemClauseType::ModuleOf),
("$module_retract_clause", 5) => Some(SystemClauseType::ModuleRetractClause),

51
src/prolog/lib/time.pl Normal file
View File

@@ -0,0 +1,51 @@
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Written April 2020 by Markus Triska (triska@metalevel.at)
Part of Scryer Prolog.
This library provides predicates for reasoning about time.
sleep/1 should be implemented here, sleeping for a number of seconds.
In addition, this library should provide reasoning about time stamps.
'$cpu_new' can be replaced by statistics/2 once that is implemented.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
:- module(time, [time/1]).
:- use_module(library(format)).
:- use_module(library(iso_ext)).
time(Goal) :-
'$cpu_now'(T0),
Goal,
'$cpu_now'(T),
Time is T - T0,
( bb_get('$first_answer', true) ->
format(" % CPU time: ~3f seconds~n", [Time])
; format("% CPU time: ~3f seconds~n ", [Time])
).
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
?- time((true;false)).
% CPU time: 0.000 seconds
true
; false.
:- time(use_module(library(clpz))).
% CPU time: 2.762 seconds
true
; false.
:- time(use_module(library(lists))).
% CPU time: 0.000 seconds
true
; false.
?- time(member(X, [a,b,c])).
% CPU time: 0.000 seconds
X = a
; % CPU time: 0.002 seconds
X = b
; % CPU time: 0.004 seconds
X = c
; false.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

View File

@@ -28,6 +28,9 @@ use std::io::{stdout, Write};
use std::iter::once;
use std::rc::Rc;
use std::time::Duration;
use cpu_time::ProcessTime;
use crate::crossterm::event::{read, Event, KeyCode, KeyEvent};
use crate::crossterm::terminal::{enable_raw_mode, disable_raw_mode};
@@ -1952,6 +1955,13 @@ impl MachineState {
self.fail = result;
}
&SystemClauseType::CPU_now => {
let a1 = self[temp_v!(1)];
let a2 = ProcessTime::now().as_duration().as_secs_f64();
let addr = self.heap.put_constant(Constant::Float(OrderedFloat(a2)));
self.unify(a1, addr);
}
&SystemClauseType::OpDeclaration => {
let priority = self[temp_v!(1)];
let specifier = self[temp_v!(2)];