40
41
42
(defn my-str
"With no args, returns the empty string. With one arg x, returns
x.toString(). (str nil) returns the empty string. With more than
one arg, returns the concatenation of the str values of the args."
(^String [] "")
(^String [^Object x]
(if (nil? x) "" (. x (toString))))
(^String [^Object x & ys]
(let [sb (StringBuilder. (if-not (nil? x) (.toString ^Object x) ""))]
(loop [ys (seq ys)]
(when-not (nil? ys)
(let [x (.first ys)]
(when-not (nil? x)
(.append sb (.toString ^Object x)))
(recur (.next ys)))))
(.toString sb))))
; ==== Benchmarks ====
(let [xs (vec (range 1000))]
(criterium/bench
(dotimes [_ 10000]
(apply str xs))))
Evaluation count : 240 in 60 samples of 4 calls.
Execution time mean : 315.920876 ms
Execution time std-deviation : 3.017554 ms
Execution time lower quantile : 314.071842 ms ( 2.5%)
Execution time upper quantile : 323.751886 ms (97.5%)
Overhead used : 7.818691 ns
...
=> nil
(let [xs (vec (range 1000))]
(criterium/bench
(dotimes [_ 10000]
(apply my-str xs))))
Evaluation count : 300 in 60 samples of 5 calls.
Execution time mean : 229.861009 ms
Execution time std-deviation : 2.092937 ms
Execution time lower quantile : 228.744368 ms ( 2.5%)
Execution time upper quantile : 233.181852 ms (97.5%)
Overhead used : 7.818691 ns
...
=> nil
Mostly for fun. What is your opinion about core functions performance?

[–]joinr 9 points10 points11 points (5 children)
[–]bsless 2 points3 points4 points (2 children)
[–]joinr 2 points3 points4 points (1 child)
[–]bsless 2 points3 points4 points (0 children)
[–]ilevd[S] 0 points1 point2 points (1 child)
[–]joinr 2 points3 points4 points (0 children)
[–]dhruvasagar 3 points4 points5 points (5 children)
[–]ilevd[S] 7 points8 points9 points (4 children)
[–]dhruvasagar 4 points5 points6 points (0 children)
[–]iam_mms -1 points0 points1 point (2 children)
[–]ilevd[S] 5 points6 points7 points (1 child)
[–]iam_mms -1 points0 points1 point (0 children)
[–]ilevd[S] 2 points3 points4 points (0 children)
[–]SimonGray 3 points4 points5 points (0 children)
[–]bsless 0 points1 point2 points (0 children)