on master help sign in
Add Clock/clockTime in System

This is for getting the time at a finer granularity than seconds. I want this for getting approximate wall clock time measurements for run time of fragments of programs, so the interface is very basic, but I am open to better, more sophisticated APIs! I expect this won't work on Windows, so it's #ifdefed out and always returns 0. If you know how to fix this, please do!

commit62bd431d58f1c2af394b684bb175328f4a44d2de authorEdwin Brady <ecb10@st-andrews.ac.uk> dateThu 25 Apr 2019, 23:18 (8 years ago) parentdd065f3
3 files changed+33
libs/base/System.idr+14 rts/idris_stdfgn.c+18 rts/idris_stdfgn.h+1
libs/base/System.idr+14
@@ -93,6 +93,20 @@ time : IO Integer
9393time = do MkRaw t <- foreign FFI_C "idris_time" (IO (Raw Integer))
9494 pure t
9595
96+||| Wall clock time
97+record Clock where
98+ constructor MkClock
99+ seconds : Integer
100+ nanoseconds : Integer
101+
102+||| Get the system's wall clock time.
103+||| At the moment, this returns 0 on Windows
104+clockTime : IO Clock
105+clockTime
106+ = do vm <- getMyVM
107+ MkRaw c <- foreign FFI_C "idris_clock" (Ptr -> IO (Raw Clock)) vm
108+ pure c
109+
96110||| Specify interval to sleep for in microseconds, must be nonnegative
97111usleep : (i : Int) -> { auto prf : So (i >= 0) } -> IO ()
98112usleep i = foreign FFI_C "idris_usleep" (Int -> IO ()) i
rts/idris_stdfgn.c+18
@@ -206,6 +206,24 @@ VAL idris_time() {
206206 return MKBIGI(t);
207207}
208208
209+VAL idris_clock(VM* vm) {
210+ VAL result;
211+#ifdef _WIN32
212+ idris_constructor(result, vm, 0, 2, 0);
213+ idris_setConArg(result, 0, MKBIGI(0));
214+ idris_setConArg(result, 1, MKBIGI(0));
215+#else
216+ struct timespec ts;
217+ // We're not checking the result here, which is of course bad, but
218+ // CLOCK_REALTIME is required by POSIX at least!
219+ clock_gettime(CLOCK_REALTIME, &ts);
220+ idris_constructor(result, vm, 0, 2, 0);
221+ idris_setConArg(result, 0, MKBIGI(ts.tv_sec));
222+ idris_setConArg(result, 1, MKBIGI(ts.tv_nsec));
223+#endif
224+ return result;
225+}
226+
209227VAL idris_mkFileError(VM* vm) {
210228 VAL result;
211229 switch(errno) {
rts/idris_stdfgn.h+1
@@ -52,6 +52,7 @@ void* idris_stdin();
5252char* getEnvPair(int i);
5353
5454VAL idris_time();
55+VAL idris_clock(VM* vm);
5556
5657void idris_forceGC();
5758