Skip to content

Instantly share code, notes, and snippets.

@zoldar zoldar/gist:6425609
Last active Dec 22, 2015

Embed
What would you like to do?
sort vs frequencies
user=> (def word "sadfasdfagrregwfagfregawgfresafafewrf")
#'user/word
user=> (time (dotimes [i 10000] (doall (sort word))))
"Elapsed time: 99.122541 msecs"
nil
user=> (time (dotimes [i 10000] (doall (sort word))))
"Elapsed time: 96.896736 msecs"
nil
user=> (time (dotimes [i 10000] (doall (sort word))))
"Elapsed time: 95.14145 msecs"
nil
user=> (time (dotimes [i 10000] (frequencies word)))
"Elapsed time: 208.046098 msecs"
nil
user=> (time (dotimes [i 10000] (frequencies word)))
"Elapsed time: 208.561108 msecs"
nil
user=> (time (dotimes [i 10000] (frequencies word)))
"Elapsed time: 201.423851 msecs"
user=> (source frequencies)
(defn frequencies
"Returns a map from distinct items in coll to the number of times
they appear."
{:added "1.2"
:static true}
[coll]
(persistent!
(reduce (fn [counts x]
(assoc! counts x (inc (get counts x 0))))
(transient {}) coll)))
nil
user=> (source sort)
(defn sort
"Returns a sorted sequence of the items in coll. If no comparator is
supplied, uses compare. comparator must implement
java.util.Comparator. If coll is a Java array, it will be modified.
To avoid this, sort a copy of the array."
{:added "1.0"
:static true}
([coll]
(sort compare coll))
([^java.util.Comparator comp coll]
(if (seq coll)
(let [a (to-array coll)]
(. java.util.Arrays (sort a comp))
(seq a))
())))
nil
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.