Skip to content

Instantly share code, notes, and snippets.

@thiagoa
Created December 30, 2019 16:31
Show Gist options
  • Save thiagoa/1ac5d848f6c01543532743269cbae600 to your computer and use it in GitHub Desktop.
Save thiagoa/1ac5d848f6c01543532743269cbae600 to your computer and use it in GitHub Desktop.
;; - unmix1 is my go-to solution
;; - unmix2 is 21% faster than unmix1
;; - unmix3 is 212% faster than unmix1
;; - unmix3 is a classic car-cdr lispy solution
;;
;; Benchmarked with criterium
(defn unmix1 [s]
(reduce (fn [memo [a b]] (str memo b a))
""
(partition-all 2 s)))
;; Results for unmix1
;;
;; Evaluation count : 5196780 in 60 samples of 86613 calls.
;; Execution time mean : 11,874229 µs
;; Execution time std-deviation : 372,407530 ns
;; Execution time lower quantile : 11,389739 µs ( 2,5%)
;; Execution time upper quantile : 12,736383 µs (97,5%)
;; Overhead used : 7,558749 ns
(defn unmix2 [s]
(loop [[a b] (take 2 s)
s (nthrest s 2)
memo ""]
(if (or a b)
(recur
(take 2 s)
(nthrest s 2)
(str memo b a))
memo)))
;; Results for unmix2
;;
;; Evaluation count : 6179220 in 60 samples of 102987 calls.
;; Execution time mean : 9,805924 µs
;; Execution time std-deviation : 219,493487 ns
;; Execution time lower quantile : 9,574716 µs ( 2,5%)
;; Execution time upper quantile : 10,450540 µs (97,5%)
;; Overhead used : 7,558749 ns
(defn unmix3 [s]
(loop [a (first s)
b (second s)
s (rest (rest s))
memo ""]
(if (or a b)
(recur
(first s)
(second s)
(rest (rest s))
(str memo b a))
memo)))
;; Results for unmix3
;;
;; Evaluation count : 15704100 in 60 samples of 261735 calls.
;; Execution time mean : 3,800963 µs
;; Execution time std-deviation : 65,247816 ns
;; Execution time lower quantile : 3,745129 µs ( 2,5%)
;; Execution time upper quantile : 3,986512 µs (97,5%)
;; Overhead used : 7,558749 ns
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment