(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