JanetDocsSourcePlaygroundTutorialsI'm Feeling luckyCommunityGitHub sign in

os/clock

core-api


    cfunction
    src/core/os.c on line 1756, column 1

    (os/clock &opt clock as)

    Return the current time of the requested clock source.

    The `clock` 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 `as` 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-tuple of integers `[seconds nanoseconds]`.


3 examplesSign in to add an example
Loading...
(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
veqqqPlayground
(string/format "%f" (os/clock)) # => "1627746483.991000"
dbreadyPlayground
(os/clock)  # => 1.59384e+09
cellularmitosisPlayground