Skip to content

Instantly share code, notes, and snippets.

@jsn
Created February 9, 2020 21:02
Show Gist options
  • Save jsn/cfbf2e51393c4cba062898e452f0d23c to your computer and use it in GitHub Desktop.
Save jsn/cfbf2e51393c4cba062898e452f0d23c to your computer and use it in GitHub Desktop.
;; in the code below, 'f' function is exactly the same as 'g' function,
;; and every line starting with '(time ...)' measures exactly the same
;; calculation. Which is for some reason fast for 'f', for as long as
;; 'g' is not defined, and after that it soon becomes slow both for 'f'
;; and for 'g'. The effect is quite reproducible.
user=> (set! *warn-on-reflection* true)
true
user=> (def xs (int-array 1 0))
#'user/xs
user=> (defn f [] (let [i (aget ^ints xs 0)] (aset ^ints xs 0 (dec i)) (if (zero? i) i f)))
#'user/f
user=> (time (do (aset ^ints xs 0 (long 1e8)) (trampoline f)))
"Elapsed time: 329.766837 msecs"
0
user=> (time (do (aset ^ints xs 0 (long 1e8)) (trampoline f)))
"Elapsed time: 328.638392 msecs"
0
user=> (time (do (aset ^ints xs 0 (long 1e8)) (trampoline f)))
"Elapsed time: 350.18006 msecs"
0
user=> (time (do (aset ^ints xs 0 (long 1e8)) (trampoline f)))
"Elapsed time: 349.447723 msecs"
0
user=> (defn g [] (let [i (aget ^ints xs 0)] (aset ^ints xs 0 (dec i)) (if (zero
? i) i g)))
#'user/g
user=> (time (do (aset ^ints xs 0 (long 1e8)) (trampoline g)))
"Elapsed time: 5039.511476 msecs"
0
user=> (time (do (aset ^ints xs 0 (long 1e8)) (trampoline f)))
"Elapsed time: 351.923112 msecs"
0
user=> (time (do (aset ^ints xs 0 (long 1e8)) (trampoline g)))
"Elapsed time: 5169.831072 msecs"
0
user=> (time (do (aset ^ints xs 0 (long 1e8)) (trampoline f)))
"Elapsed time: 4869.559602 msecs"
0
user=> (time (do (aset ^ints xs 0 (long 1e8)) (trampoline g)))
"Elapsed time: 5179.243801 msecs"
0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment