Skip to content

Instantly share code, notes, and snippets.

@swannodette
Created August 21, 2011 17:04
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 swannodette/1160849 to your computer and use it in GitHub Desktop.
Save swannodette/1160849 to your computer and use it in GitHub Desktop.
vecmatch.clj
(def IntArray (class (int-array [])))
(defmethod vtest-inline ::ints
[_ ocr] `(instance? IntArray ~ocr))
(defmethod vnth-inline ::ints
[_ ocr i] `(aget ~ocr ~i))
;; 60ms
(let [x (int-array [1 2 3])]
(dotimes [_ 10]
(time
(dotimes [_ 1e7]
(match [x]
[([_ _ 2] :vec ::ints)] :a0
[([1 1 3] :vec ::ints)] :a1
[([1 2 3] :vec ::ints)] :a2)))))
(defmethod vtest-inline ::bits
[_ ocr] `(instance? Long ~ocr))
(defmethod vnth-inline ::bits
[_ ocr i] `(bit-shift-right
(bit-and ~ocr (bit-shift-left 1 ~i)) ~i))
;; 50ms
(let [x 5]
(dotimes [_ 10]
(time
(dotimes [_ 1e7]
(match [x]
[([_ _ 1 1] :vec ::bits)] :a0
[([1 0 1 _] :vec ::bits)] :a1)))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment