Timing analysis of Clojure code -
hello clojure experts,
i trying timing tests in clojure 1.3 , thought i'd ask question based on existing piece of code solves differential equation adapted blog post.
code follows:
;; differential equation ;; dy/dt = f(y,t) = t - y (defn f [t y] (- t y)) ;; solve using euler's method (defn solveeuler [t0 y0 h iter] (if (> iter 0) (let [t1 (+ t0 h) y1 (+ y0 (* h (f t0 y0)))] (recur t1 y1 h (dec iter))) [t0 y0 h iter])) (defn multiplesolveeuler [] (let [steps '(1 10 100 1000 10000 100000) results (map #(second (solveeuler 0.0 0.0 (/ 1.0 %) %)) steps) errors (map #(- (math/exp -1) %) results)] (partition 3 (interleave steps results errors)))) (def *cpuspeed* 2.0) (defmacro cyclesperit [expr its] `(let [start# (. system (nanotime)) ret# ( ~@expr (/ 1.0 ~its) ~its ) finish# (. system (nanotime))] (int (/ (* *cpuspeed* (- finish# start#)) ~its)))) (defn solveeuler-2 [t0 y0 h its] (let [zero (int 0)] (loop [t0 (double t0), y0 (double y0), h (double h), (int its)] (if (> zero) (let [t1 (+ t0 h) y1 (+ y0 (* h (- t0 y0)))] (recur t1 y1 h (dec its))) [t0 y0 h its]))))
so when
(time solveiteuler-2 0.0 1.0 (/ 1.0 1000000000) 1000000000))
i time 6004.184 msecs on 6 month old macbook pro. issue command again, , around same time. when run 3 other times times in range of 3500 msec. have noticed before other code snippets, , wondering why so. suppose expect similar execution time across successive runs.
is lack of understanding how "time" works, missing, or kind of caching happening under hood?
thanks.
it's not time
that's relevant, rather jvm optimizing code @ runtime. observe typical; execution time drop , stabilize after 3 invocations.
Comments
Post a Comment