Skip to content

Instantly share code, notes, and snippets.

@kirasystems
Created December 3, 2012 22:57
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kirasystems/4198891 to your computer and use it in GitHub Desktop.
Save kirasystems/4198891 to your computer and use it in GitHub Desktop.
(defrecord Fragment [t p1 p2])
(defprotocol FragmentProtocol
(t [fragment] "Return the type.")
(switch-type [fragment] "Switch type."))
(deftype TypeFragment [f-type p1 p2]
FragmentProtocol
(t [this] f-type)
(switch-type [this] (TypeFragment. (if (= f-type :type-a) :type-a :type-b) p1 p2)))
(deftype TypeFragmentA [p1 p2])
(deftype TypeFragmentB [p1 p2])
(extend-type TypeFragmentA
FragmentProtocol
(t [this] :type-a)
(switch-type [this] (TypeFragmentB. (.p1 this) (.p2 this))))
(extend-type TypeFragmentB
FragmentProtocol
(t [this] :type-b)
(switch-type [this] (TypeFragmentA. (.p1 this) (.p2 this))))
#_(use 'criterium.core)
#_(bench
(loop [x (Fragment. :type-a 1 2)
y 20000]
(if-not (zero? y)
(case (:t x)
:type-a (recur (assoc x :t :type-b) (unchecked-dec y))
:type-b (recur (assoc x :t :type-a) (unchecked-dec y))))))
;Evaluation count : 115980 in 60 samples of 1933 calls.
;Execution time mean : 454.118486 us
;Execution time std-deviation : 21.492973 us
;Execution time lower quantile : 431.526125 us ( 2.5%)
;Execution time upper quantile : 507.327121 us (97.5%)
;
;Found 4 outliers in 60 samples (6.6667 %)
;low-severe 2 (3.3333 %)
;low-mild 2 (3.3333 %)
;Variance from outliers : 33.5759 % Variance is moderately inflated by outliers
#_(bench
(loop [x (TypeFragment. :type-a 1 2)
y 20000]
(if-not (zero? y)
(case (t x)
:type-a (recur (TypeFragment. :type-b (.p1 x) (.p2 x)) (unchecked-dec y))
:type-b (recur (TypeFragment. :type-a (.p1 x) (.p2 x)) (unchecked-dec y))))))
;Evaluation count : 242760 in 60 samples of 4046 calls.
;Execution time mean : 261.116716 us
;Execution time std-deviation : 26.578590 us
;Execution time lower quantile : 237.615175 us ( 2.5%)
;Execution time upper quantile : 316.372368 us (97.5%)
#_(bench
(loop [x (TypeFragmentA. 1 2)
y 20000]
(if-not (zero? y)
(recur (switch-type x) (unchecked-dec y)))))
;Evaluation count : 57240 in 60 samples of 954 calls.
;Execution time mean : 965.763854 us
;Execution time std-deviation : 105.626356 us
;Execution time lower quantile : 849.924502 us ( 2.5%)
;Execution time upper quantile : 1.177628 ms (97.5%)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment