cfunction
src/core/os.c on line 1685 , column 1
(os/clock &opt source format )
Return the current time of the requested clock source.
The `source` argument selects the clock source to use , when not
specified the default is `:realtime` :
* :realtime: Return the real (i.e. , wall-clock ) time. This clock is
affected by discontinuous jumps in the system time
* :monotonic: Return the number of whole + fractional seconds since
some fixed point in time. The clock is guaranteed to be
non-decreasing in real time.
* :cputime: Return the CPU time consumed by this process (i.e. all
threads in the process )
The `format` argument selects the type of output , when not
specified the default is `:double` :
* :double: Return the number of seconds + fractional seconds as a
double
* :int: Return the number of seconds as an integer
* :tuple: Return a 2 integer tuple [seconds , nanoseconds ]
(defn bench `Feed bench a wrapped func and int, receive int for time in ns`
[thunk times ]
(def start (os/clock :cputime :tuple ))
(loop [_ :range [times ]]
(thunk ))
(def end (os/clock :cputime :tuple ))
(/ (+ (* (- (end 0 ) (start 0 )) 1e9 )
(- (end 1 ) (start 1 )))
times ))
# it turns out os/clock is pretty darn fast (comparatively)
(def iterations 2000000 )
(bench |(os/clock :cputime :tuple ) iterations ) # 1283.30053 ns
(bench |(slurp "/proc/self/schedstat" ) iterations ) # 7881.451760 ns
# these basically benchmark slurp
(bench |(do (def f (file/open "/proc/self/schedstat" :r ))
(def content (file/read f :all ))
(file/close f ))
iterations ) # 4894.832760 ns
# even without opening and closing the file, reading in Janet's slower than os/clock
(def f (file/open "/proc/self/schedstat" :r ))
(bench |(do (file/seek f :set 0 )
(def content (file/read f :all ))) iterations ) # 1802.511470 ns
(file/close f )
# Of course bench has some overhead, but it's amortized across iterations anyway
(bench (fn []) 10000000 ) # 42.030338 ns
(string/format "%f" (os/clock )) # => "1627746483.991000"
(os/clock ) # => 1.59384e+09