Last active
December 22, 2015 05:39
-
-
Save zoldar/6425609 to your computer and use it in GitHub Desktop.
sort vs frequencies
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
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