Skip to content

Instantly share code, notes, and snippets.

@athos
Created July 9, 2013 12:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save athos/5956979 to your computer and use it in GitHub Desktop.
Save athos/5956979 to your computer and use it in GitHub Desktop.
^:dynamicなVarの参照にかかる時間
user=> (def ^:dynamic x 0)
#'user/x
user=> (dotimes [_ 5] (time (dotimes [_ 10000000] x)))
"Elapsed time: 665.329 msecs"
"Elapsed time: 755.922 msecs"
"Elapsed time: 735.566 msecs"
"Elapsed time: 780.893 msecs"
"Elapsed time: 766.333 msecs"
nil
user=> (binding [x 100] x) ; 一度値を動的に束縛すると、それ以降の参照は遅くなる
100
user=> (dotimes [_ 5] (time (dotimes [_ 10000000] x)))
"Elapsed time: 2802.534 msecs"
"Elapsed time: 2907.016 msecs"
"Elapsed time: 2870.731 msecs"
"Elapsed time: 2910.89 msecs"
"Elapsed time: 3022.429 msecs"
nil
user=> (def y 0)
#'user/y
user=> (dotimes [_ 5] (time (dotimes [_ 10000000] y)))
"Elapsed time: 758.419 msecs"
"Elapsed time: 802.252 msecs"
"Elapsed time: 828.285 msecs"
"Elapsed time: 801.893 msecs"
"Elapsed time: 830.19 msecs"
nil
user=>
(comment
以上の結果から次のことがいえる:
- ^:dynamicでないVarの参照は速い
- ^:dynamicなVarの参照は、動的束縛されるまでは^:dynamicでないVarと同じくらい速い
- ^:dynamicなVarを一度動的束縛すると、以降の参照は数倍レベルで遅くなる
ちなみに、現在のClojureの実装では、参照にかかる時間は一度遅くなると元に戻らない。
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment