Created
April 6, 2014 16:44
-
-
Save si14/10008500 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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