Skip to content

Instantly share code, notes, and snippets.

@si14
Created April 6, 2014 16:44
Show Gist options
  • Save si14/10008500 to your computer and use it in GitHub Desktop.
Save si14/10008500 to your computer and use it in GitHub Desktop.
(ns testapp.core
(:require
[criterium.core :as cr]
[primitive-math :as p]
[no.disassemble :as nd]))
(set! *warn-on-reflection* true)
(defn ^double test0 [a b]
(let [^doubles a a
^doubles b b
n (alength a)]
(loop [i (int 0)
acc (double 0)]
(if-not (< i n) acc
(recur (p/+ i (int 1))
(p/+ acc
(let [a-x (aget a i)
b-x (aget b i)]
(if (p/> a-x b-x)
(p/- a-x b-x)
(p/- b-x a-x)))))))))
(defn bench0 []
(let [a (double-array (range 1 100000))
b (double-array (range 100000 1 -1))]
(cr/quick-bench (test0 a b))))
(defn ^double test1 [a b]
(let [^doubles a a
^doubles b b]
(areduce a i ret (double 0)
(p/+ ret
(let [a-x (aget a i)
b-x (aget b i)]
(if (p/> a-x b-x)
(p/- a-x b-x)
(p/- b-x a-x)))))))
(defn bench1 []
(let [a (double-array (range 1 100000))
b (double-array (range 100000 1 -1))]
(cr/quick-bench (test1 a b))))
(defn ^double abs-diff [^double x ^double y]
(if (p/> x y) (p/- x y) (p/- y x)))
(defn ^double test2 [a b]
(let [^doubles a a
^doubles b b]
(areduce a i ret (double 0)
(p/+ ret (double (abs-diff (aget a i) (aget b i)))))))
(defn bench2 []
(let [a (double-array (range 1 100000))
b (double-array (range 100000 1 -1))]
(cr/quick-bench (test2 a b))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment