I was wondering if there is a way to generate a fixed length random string in Clojure.
A quick search resulted in:
https://gist.github.com/rboyd/5053955
(defn rand-str [len]
(apply str (take len (repeatedly #(char (+ (rand 26) 65))))))
After I had a look at the VisualVM CPU profile data I realized that is consumes a non-trivial amount of CPU:
I decided to rewrite it be similar to the Java function I have used to use:
(defn rand-str2-slow
^String [^Long len]
(let [leftLimit 97
rightLimit 122
random (Random.)
stringBuilder (StringBuilder. len)
diff (- rightLimit leftLimit)]
(dotimes [_ len]
(let [ch (char (.intValue (+ leftLimit (* (.nextFloat random) (+ diff 1)))))]
(.append stringBuilder ch)))
(.toString stringBuilder)))
This resulted in even slower code, however the stack trace has much less depth:
I noticed it does a lot of Reflector.getMethods(). Is there a way to type hint the function to avoid that?
UPDATE1:
Relevant microbenchmarks:
rand-str
(with-progress-reporting (quick-bench (rand-str 5000) :verbose))
Execution time sample mean : 1.483232 ms
Execution time mean : 1.483547 ms
Execution time sample std-deviation : 31.161960 µs
Execution time std-deviation : 31.651732 µs
Execution time lower quantile : 1.441678 ms ( 2.5%)
Execution time upper quantile : 1.531289 ms (97.5%)
Overhead used : 14.598226 ns
rand-str2-slow
(with-progress-reporting (quick-bench (rand-str2-slow 5000) :verbose))
Execution time sample mean : 17.637256 ms
Execution time mean : 17.647974 ms
Execution time sample std-deviation : 523.527242 µs
Execution time std-deviation : 528.559280 µs
Execution time lower quantile : 17.322583 ms ( 2.5%)
Execution time upper quantile : 18.522246 ms (97.5%)
Overhead used : 14.598226 ns
rand-str2 (fast)
(with-progress-reporting (quick-bench (rand-str2 5000) :verbose))
Execution time sample mean : 84.362974 µs
Execution time mean : 84.355379 µs
Execution time sample std-deviation : 3.496944 µs
Execution time std-deviation : 3.674542 µs
Execution time lower quantile : 80.911920 µs ( 2.5%)
Execution time upper quantile : 89.264431 µs (97.5%)
Overhead used : 14.598226 ns
Let me answer my own question:
(defn rand-str2
^String [^Long len]
(let [leftLimit 97
rightLimit 122
random (Random.)
stringBuilder (StringBuilder. len)
diff (- rightLimit leftLimit)]
(dotimes [_ len]
(let [ch (char (.intValue ^Double (+ leftLimit (* (.nextFloat ^Random random) (+ diff 1)))))]
(.append ^StringBuilder stringBuilder ch)))
(.toString ^StringBuilder stringBuilder)))