Skip to content

Instantly share code, notes, and snippets.

@mkwatson
Last active August 29, 2015 14:15
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 mkwatson/8d835b9dfa5cb5f37b73 to your computer and use it in GitHub Desktop.
Save mkwatson/8d835b9dfa5cb5f37b73 to your computer and use it in GitHub Desktop.
reducers string/join
(defn str-join
([] nil)
(^String [^String x ^String y]
(if (and x y) (.. (new StringBuilder x)
(append "\n")
(append y)
toString)
(if x x
(if y y)))))
(defn str-join-2
[strs]
(clojure.string/join "\n" strs))
(defn str-join-3
([] nil)
([x y]
(if (and x y) (str x "\n" y)
(if x (str x)
(if y (str y))))))
(require '[clojure.core.reducers :as r])
(require '[criterium.core :refer [quick-bench bench]])
(quick-bench (r/fold str-join ["1" "2" "3" "4" "5"]))
;; Execution time mean : 490.672257 ns
;; Execution time std-deviation : 71.757578 ns
;; Execution time lower quantile : 447.661789 ns ( 2.5%)
;; Execution time upper quantile : 610.315252 ns (97.5%)
;; Overhead used : 112.290565 ns`
(quick-bench (r/fold str-join-3 ["1" "2" "3" "4" "5"]))
;; Execution time mean : 1.239270 µs
;; Execution time std-deviation : 118.953022 ns
;; Execution time lower quantile : 1.100272 µs ( 2.5%)
;; Execution time upper quantile : 1.365724 µs (97.5%)
;; Overhead used : 112.290565 ns
(quick-bench (r/fold str-join (repeat 100000 "test")))
;; Execution time mean : 17.564645 sec
;; Execution time std-deviation : 120.251706 ms
;; Execution time lower quantile : 17.495060 sec ( 2.5%)
;; Execution time upper quantile : 17.771489 sec (97.5%)
;; Overhead used : 112.290565 ns
(quick-bench (r/fold str-join-3 (repeat 100000 "test")))
;; Execution time mean : 18.218646 sec
;; Execution time std-deviation : 537.798381 ms
;; Execution time lower quantile : 17.459723 sec ( 2.5%)
;; Execution time upper quantile : 18.768461 sec (97.5%)
;; Overhead used : 112.290565 ns
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment