Skip to content

Instantly share code, notes, and snippets.

@si14

si14/fast.clj Secret

Created September 14, 2013 14:12
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 si14/737920b513fb8ccae152 to your computer and use it in GitHub Desktop.
Save si14/737920b513fb8ccae152 to your computer and use it in GitHub Desktop.
;; 5 microsecs
(loop [a-row (int 0)
a-idx a-offset
b-idx b-offset
c-idx c-offset]
(when (< a-row a-rows)
(loop [a-col (int 0)
a-idx a-idx
b-idx b-idx
c-idx c-idx]
(when (< a-col a-cols)
(aset c-data c-idx
(* (aget a-data a-idx)
(aget b-data b-idx)))
(recur (inc a-col)
(+ a-idx (aget c-strides 1))
(+ b-idx (aget c-strides 1))
(+ c-idx (aget c-strides 1)))))
(recur (inc a-row)
(+ a-idx (aget a-strides 0))
(+ b-idx (aget b-strides 0))
(+ c-idx (aget c-strides 0)))))
;; 20 microsecs
(loop [a-row (int 0)
a-idx a-offset
b-idx b-offset
c-idx c-offset
acc nil]
(if (< a-row a-rows)
(let [new-acc
(loop [a-col (int 0)
a-idx a-idx
b-idx b-idx
c-idx c-idx
acc acc]
(if (< a-col a-cols)
(let [new-acc
(aset c-data c-idx
(* (aget a-data a-idx)
(aget b-data b-idx)))]
(recur (inc a-col)
(+ a-idx (aget c-strides 1))
(+ b-idx (aget c-strides 1))
(+ c-idx (aget c-strides 1))
new-acc))
acc))]
(recur (inc a-row)
(+ a-idx (aget a-strides 0))
(+ b-idx (aget b-strides 0))
(+ c-idx (aget c-strides 0))
new-acc))
acc))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment