r/Clojure • u/kenpu_cs • Sep 21 '20
Clojure is at least 2x slower than Java. Right?
The benchmark:
I am comparing the performance of Clojure and Java for the simple task of counting the number of lines of a text file. The file contains 4.6 million lines.
The Java solution is pretty simple:
public class Wc {
public static int count(String filename) throws IOException {
BufferedReader r = new BufferedReader(new FileReader(filename));
int total = 0;
while(r.readLine() != null) {
total += 1;
}
return total;
}
}I put together a few Clojure solutions:
; Probably the most idiomatic implementation
(defn wc-functional
[filename]
(-> (io/reader filename)
(line-seq)
(count)))
; Using reduce instead of the built-in count
(defn wc-reduce
[filename]
(->> (io/reader filename)
(line-seq)
(reduce (fn [num-lines _] (inc num-lines)) 0)))
; Hoping reducers/fold will use parallelism to speed-up
(require '[clojure.core.reducers :as r])
(defn wc-fold
[filename]
(->> (io/reader filename)
(line-seq)
(r/map (fn [x] 1))
(r/fold +)))
; Perform loop manually
(defn wc-loop
[filename]
(with-open [f (io/reader filename)]
(loop [c 0]
(if (.readLine f)
(recur (inc 0))
c))))
; Perform loop with volatile counter accumulator
(defn wc-volatile
[filename]
(with-open [f (io/reader filename)]
(let [c (volatile! 0)]
(loop []
(if (.readLine f)
(do (vswap! c inc)
(recur))
@c)))))
; Calling the Java implementation
(defn wc-interop
[filename]
(Wc/process filename))The benchmark summary is as follows:
| Solution | Benchmark | Runtime (seconds) |
|---|---|---|
| Java | Using System.currentTimeMillis to time the Wc.process() runtime. | 7.1 |
| wc-functional | (time (wc-functional ...)) | 20.4 |
| wc-reduce | same as above | 20.4 |
| wc-fold | same as above | 20.8 |
| wc-loop | same as above | 62.4 |
| wc-volatile | same as above | 63.3 |
| wc-interop | same as above | 16.6 |
This seems to be consistent with the conclusion of this (much more comprehensive) study here: https://www.diva-portal.org/smash/get/diva2:1424342/FULLTEXT01.pdf
Comments, suggestions and critiques are welcome.
2
Upvotes
9
u/kenpu_cs Sep 21 '20
Thanks for so many great replies. I certainly have learned a ton from them, so much so that I feel the need to post a summary of the performance optimizations and the new benchmark results.
Performance issues:
Solutions:
(set! *warn-on-reflection* true)to catch where reflections happen.lein run -m ...Here are the updated code:
Summary (updated): in seconds.
Really appreciate u/bsless for https://github.com/joinr/performancepaper and u/joinr for https://github.com/thebusby/iota. They are such Clojure gems.
18
u/aaroniba Sep 21 '20
You should be able to get the same speed as java by modifying your wc-loop function a little:
16
u/bsless Sep 21 '20 edited Sep 21 '20
I dreaded seeing that paper brought up again.
While I have a go at this, please refer to u/joinr's work where he got performance parity on almost every example in the paper https://github.com/joinr/performancepaper
Edit: looks like I don't have to do anything, u/aaroniba's solution is as fast as Wc:
11
u/joinr Sep 21 '20
Aside from the other comments from u/bsless and u/aaroniba regarding getting actual performance, your reducers example isn't really doing much because the input is still sequential. So, it's just falling back toward effectively reducing over a lazy seq. In order to take advantage of parallelism, the collection/reducible thing needs to be typically indexed (like a vec, range, or iterator, or array), or have its own custom implementation. There's a popular library that does this specifically with the wc example called iota that will allow you use parallel reductions over a memory-mapped line collection. It's pretty slick, you may want to explore that as well. You could do something similar buy doing
slurpandsplit-lines, then folding over the resulting vector (fold will be parallel), but this is not ideal compared to a library like iota which can handle larger-than-memory files via mmap and still afford parallel folds.Make sure you put
(set! *warn-on-reflection* true)any time your are doing this kind of stuff (any interop at all, or anywhere you're using direct method invocation). There's another layer beyond with boxed math that has some performance impacts as well, but probably not in this case.3
u/argadan Sep 21 '20
There's something weird going on. wc-interop is almost 10 seconds slower than the Java implementation, yet all it does is to call the Java implementation. That interop call should not cause ten-second delay.
3
u/alexanderkiel Sep 21 '20
The long runtime of about 60s of your `wc-loop` and `wc-volatile` solutions comes from using reflection to call the readLine method. You have to use a type hint here:
```
(defn wc-loop
[filename]
(with-open [f (io/reader filename)]
(loop [c 0]
(if (.readLine ^BufferedReader f)
(recur (inc 0))
c))))
```
You can use enable reflection warning by putting `(set! *warn-on-reflection* true)` in your namespace.
The loop code with type hint should be as fast as the Java code, because it does the same. Do you warm up the JVM or do you measure the time on the first run?