Created
December 2, 2016 09:18
-
-
Save wagjo/f24f8d4a29d5d3c87e8119c8e306883a to your computer and use it in GitHub Desktop.
CLJ-1912 benchmark
This file contains 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 clj1912.core | |
(:require [criterium.core :refer :all])) | |
(defn new= | |
"Equality. Returns true if x equals y, false if not. Same as | |
Java x.equals(y) except it also works for nil, and compares | |
numbers and collections in a type-independent manner. Clojure's immutable data | |
structures define equals() (and thus =) as a value, not an identity, | |
comparison." | |
{:inline (fn [x y] `(. clojure.lang.Util equiv ~x ~y)) | |
:inline-arities #{2} | |
:added "1.0"} | |
([x] true) | |
([x y] (clojure.lang.Util/equiv x y)) | |
([x y & more] | |
(if (clojure.lang.Util/equiv x y) | |
(let [nmore (next more)] | |
(if nmore | |
(recur y (first more) nmore) | |
(clojure.lang.Util/equiv y (first more)))) | |
false))) | |
(defn new< | |
"Returns non-nil if nums are in monotonically increasing order, | |
otherwise false." | |
{:inline (fn [x y] `(. clojure.lang.Numbers (lt ~x ~y))) | |
:inline-arities #{2} | |
:added "1.0"} | |
([x] true) | |
([x y] (. clojure.lang.Numbers (lt x y))) | |
([x y & more] | |
(if (< x y) | |
(let [nmore (next more)] | |
(if nmore | |
(recur y (first more) nmore) | |
(< y (first more)))) | |
false))) | |
(defn new== | |
"Returns non-nil if nums all have the equivalent | |
value (type-independent), otherwise false" | |
{:inline (fn [x y] `(. clojure.lang.Numbers (equiv ~x ~y))) | |
:inline-arities #{2} | |
:added "1.0"} | |
([x] true) | |
([x y] (. clojure.lang.Numbers (equiv x y))) | |
([x y & more] | |
(if (== x y) | |
(let [nmore (next more)] | |
(if nmore | |
(recur y (first more) nmore) | |
(== y (first more)))) | |
false))) | |
(comment | |
*clojure-version* ;; {:major 1, :minor 9, :incremental 0, :qualifier "alpha14"} | |
(bench (< 1 2 3 4)) ;; 140.726376 ns | |
(bench (new< 1 2 3 4)) ;; 141.661964 ns | |
(bench (< 1 2 3 4 5 6 7 8 9 10)) ;; 505.381596 ns | |
(bench (new< 1 2 3 4 5 6 7 8 9 10)) ;; 460.331840 ns | |
(bench (apply < (range 100))) ;; 9.020666 µs | |
(bench (apply new< (range 100))) ;; 8.604638 µs | |
(bench (apply < (range 10000))) ;; 885.361898 µs | |
(bench (apply new< (range 10000))) ;; 851.344031 µs | |
(bench (= 1 1 1 1)) ;; 86.114371 ns | |
(bench (new= 1 1 1 1)) ;; 86.874012 ns | |
(bench (= 1 1 1 1 1 1 1 1 1 1)) ;; 333.438530 ns | |
(bench (new= 1 1 1 1 1 1 1 1 1 1)) ;; 300.628516 ns | |
(bench (apply = (repeat 100 1))) ;; 4.282752 µs | |
(bench (apply new= (repeat 100 1))) ;; 3.650438 µs | |
(bench (apply = (repeat 10000 1))) ;; 397.401808 µs | |
(bench (apply new= (repeat 10000 1))) ;; 353.294723 µs | |
(bench (== 1 1 1 1)) ;; 138.162620 ns | |
(bench (new== 1 1 1 1)) ;; 135.759047 ns | |
(bench (== 1 1 1 1 1 1 1 1 1 1)) ;; 487.963993 ns | |
(bench (new== 1 1 1 1 1 1 1 1 1 1)) ;; 460.982411 ns | |
(bench (apply == (repeat 100 1))) ;; 5.587064 µs | |
(bench (apply new== (repeat 100 1))) ;; 5.273621 µs | |
(bench (apply == (repeat 10000 1))) ;; 565.031286 µs | |
(bench (apply new== (repeat 10000 1))) ;; 537.789795 µs | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment