Skip to content

Instantly share code, notes, and snippets.

@serioga
Created December 26, 2018 08:51
Show Gist options
  • Save serioga/b2ca3f941d5fee2bd8a8f3277ab7ba17 to your computer and use it in GitHub Desktop.
Save serioga/b2ca3f941d5fee2bd8a8f3277ab7ba17 to your computer and use it in GitHub Desktop.
Test solutions for request from @linchk https://t.me/clojure_ru/71619
(ns dev.test
"Test solutions for request from @LinchK https://t.me/clojure_ru/71619"
(:require
[criterium.core :as criterium]))
(set! *warn-on-reflection* true)
(def sample
"Data sample from https://t.me/clojure_ru/71619"
[{:word "Батон" :price "10"}
{:word "Батон" :price "20"}
{:word "Питон" :price "100"}
{:word "Батон" :price "30"}])
(def sample-plus
"Extended data sample similar to 10000 times of the `sample`."
(doall (take 40000 (cycle sample))))
; Benchmarking notes:
; - measure 10000 passes over short `sample` vs single pass over long `sample-plus`
; - apply `count` to result to realise possible laziness.
(defn reduce-vec
"Reduction over vector by Sergey Trofimov https://t.me/clojure_ru/71662"
[in]
(reduce
(fn
[out item]
(let [prev-word (:word (peek out))
item-word (:word item)]
(conj out (if (= item-word prev-word)
(assoc item :word "")
item))))
[] in))
(comment
(reduce-vec sample)
(criterium/bench
(dotimes [_ 10000]
(count (reduce-vec sample))))
;Evaluation count : 7920 in 60 samples of 132 calls.
; Execution time mean : 7,765067 ms
; Execution time std-deviation : 129,669651 µs
; Execution time lower quantile : 7,625980 ms ( 2,5%)
; Execution time upper quantile : 8,005156 ms (97,5%)
; Overhead used : 2,586611 ns
(criterium/bench
(count (reduce-vec sample-plus)))
;Evaluation count : 7380 in 60 samples of 123 calls.
; Execution time mean : 8,199793 ms
; Execution time std-deviation : 126,857997 µs
; Execution time lower quantile : 8,073789 ms ( 2,5%)
; Execution time upper quantile : 8,490839 ms (97,5%)
; Overhead used : 2,586611 ns
nil)
(defn partition-mapcat-concat
"Solution by @doseke https://t.me/clojure_ru/71650"
[in]
(->> in
(partition-by :word)
(mapcat (fn [[x & xs]]
(concat [x] (map #(assoc % :word "") xs))))))
(comment
(partition-mapcat-concat sample)
(criterium/bench
(dotimes [_ 10000]
(count (partition-mapcat-cons sample))))
;Evaluation count : 1680 in 60 samples of 28 calls.
; Execution time mean : 36,342796 ms
; Execution time std-deviation : 558,879555 µs
; Execution time lower quantile : 35,568065 ms ( 2,5%)
; Execution time upper quantile : 37,496070 ms (97,5%)
; Overhead used : 2,586611 ns
(criterium/bench
(count (partition-mapcat-concat sample-plus)))
;Evaluation count : 1980 in 60 samples of 33 calls.
; Execution time mean : 31,664843 ms
; Execution time std-deviation : 379,182052 µs
; Execution time lower quantile : 31,142212 ms ( 2,5%)
; Execution time upper quantile : 32,495232 ms (97,5%)
; Overhead used : 2,586611 ns
nil)
(defn partition-mapcat-cons
"Solution by @dvshilov https://t.me/clojure_ru/71652"
[in]
(->> in
(partition-by :word)
(mapcat (fn [[x & xs]]
(cons x
(map #(assoc % :word "") xs))))))
(comment
(partition-mapcat-cons sample)
(criterium/bench
(dotimes [_ 10000]
(count (partition-mapcat-cons sample))))
;Evaluation count : 1800 in 60 samples of 30 calls.
; Execution time mean : 35,018346 ms
; Execution time std-deviation : 845,327475 µs
; Execution time lower quantile : 34,146420 ms ( 2,5%)
; Execution time upper quantile : 37,212007 ms (97,5%)
; Overhead used : 2,586611 ns
(criterium/bench
(count (partition-mapcat-cons sample-plus)))
;Evaluation count : 2460 in 60 samples of 41 calls.
; Execution time mean : 25,310344 ms
; Execution time std-deviation : 331,549442 µs
; Execution time lower quantile : 24,926374 ms ( 2,5%)
; Execution time upper quantile : 26,224229 ms (97,5%)
; Overhead used : 2,586611 ns
nil)
(defn direct-loop
"Loop-recur by Sergey Trofimov https://t.me/clojure_ru/71697"
[in]
(loop [[item & tail] in
prev-word nil
out []]
(if item
(let [item-word (:word item)]
(recur tail item-word (conj out (if (= item-word prev-word)
(assoc item :word "")
item))))
out)))
(comment
(direct-loop sample)
(criterium/bench
(dotimes [_ 10000]
(count (direct-loop sample))))
;Evaluation count : 10020 in 60 samples of 167 calls.
; Execution time mean : 6,084544 ms
; Execution time std-deviation : 102,773194 µs
; Execution time lower quantile : 5,962681 ms ( 2,5%)
; Execution time upper quantile : 6,311488 ms (97,5%)
; Overhead used : 2,586611 ns
(criterium/bench
(count (direct-loop sample-plus)))
;Evaluation count : 10020 in 60 samples of 167 calls.
; Execution time mean : 6,117653 ms
; Execution time std-deviation : 120,338167 µs
; Execution time lower quantile : 5,982142 ms ( 2,5%)
; Execution time upper quantile : 6,449336 ms (97,5%)
; Overhead used : 2,586611 ns
nil)
(defn partition-window
"Sliding partition by Sergey Trofimov"
[in]
(->>
(cons nil in)
(partition 2 1)
(map
(fn [[prev item]]
(if (= (:word prev) (:word item))
(assoc item :word "")
item)))))
(comment
(partition-window sample)
(criterium/bench
(dotimes [_ 10000]
(count (partition-window sample))))
;Evaluation count : 1440 in 60 samples of 24 calls.
; Execution time mean : 42,405691 ms
; Execution time std-deviation : 1,112994 ms
; Execution time lower quantile : 41,273511 ms ( 2,5%)
; Execution time upper quantile : 45,025903 ms (97,5%)
; Overhead used : 2,586611 ns
(criterium/bench
(count (partition-window sample-plus)))
;Evaluation count : 1620 in 60 samples of 27 calls.
; Execution time mean : 38,687513 ms
; Execution time std-deviation : 979,730007 µs
; Execution time lower quantile : 37,679957 ms ( 2,5%)
; Execution time upper quantile : 40,713257 ms (97,5%)
; Overhead used : 2,586611 ns
nil)
(defn partition-map-indexed
"Solution by Dima Fomin https://t.me/clojure_ru/71751"
[in]
(->>
(partition-by :word in)
(map (fn [z]
(map-indexed (fn [i x]
(if (= 0 i)
x
(assoc x :word "")))
z)))
flatten))
(comment
(partition-map-indexed sample)
(criterium/bench
(dotimes [_ 10000]
(count (partition-map-indexed sample))))
;Evaluation count : 780 in 60 samples of 13 calls.
; Execution time mean : 81,098005 ms
; Execution time std-deviation : 3,357684 ms
; Execution time lower quantile : 78,722208 ms ( 2,5%)
; Execution time upper quantile : 91,015973 ms (97,5%)
; Overhead used : 2,586611 ns
(criterium/bench
(count (partition-map-indexed sample-plus)))
;Evaluation count : 960 in 60 samples of 16 calls.
; Execution time mean : 62,838010 ms
; Execution time std-deviation : 1,190115 ms
; Execution time lower quantile : 61,589055 ms ( 2,5%)
; Execution time upper quantile : 65,631965 ms (97,5%)
; Overhead used : 2,586611 ns
nil)
(defn using-mapv
"Solution by Dima Fomin https://t.me/clojure_ru/71753"
[in]
(mapv
(fn [x x1]
(if (not= (:word x1) (:word x))
x
(assoc x :word "")))
in (cons {} in)))
(comment
(using-mapv sample)
(criterium/bench
(dotimes [_ 10000]
(count (using-mapv sample))))
;Evaluation count : 4380 in 60 samples of 73 calls.
; Execution time mean : 13,261935 ms
; Execution time std-deviation : 230,263319 µs
; Execution time lower quantile : 12,981837 ms ( 2,5%)
; Execution time upper quantile : 13,717118 ms (97,5%)
; Overhead used : 2,586611 ns
(criterium/bench
(count (using-mapv sample-plus)))
;Evaluation count : 6000 in 60 samples of 100 calls.
; Execution time mean : 10,162208 ms
; Execution time std-deviation : 164,513415 µs
; Execution time lower quantile : 9,970641 ms ( 2,5%)
; Execution time upper quantile : 10,545334 ms (97,5%)
; Overhead used : 2,586611 ns
nil)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment