Skip to content

Instantly share code, notes, and snippets.

@mishok13
Last active December 26, 2015 05:39
Show Gist options
  • Save mishok13/7102652 to your computer and use it in GitHub Desktop.
Save mishok13/7102652 to your computer and use it in GitHub Desktop.
Line by line reading
user> (defn c [p]
(with-open [rdr (clojure.java.io/reader p)]
(reduce + (map count (line-seq rdr)))))
#'user/c
user> (bench/quick-bench
(c "/tmp/test"))
WARNING: Final GC required 356.7754050400232 % of runtime
Evaluation count : 6 in 6 samples of 1 calls.
Execution time mean : 525.040480 ms
Execution time std-deviation : 29.017225 ms
Execution time lower quantile : 491.257955 ms ( 2.5%)
Execution time upper quantile : 563.009360 ms (97.5%)
Overhead used : 2.812543 ns
nil
In [1]: def c(p):
...: with open(p) as f:
...: return sum(len(line.strip()) for line in f)
...:
In [10]: %timeit -n100 c("/tmp/test")
100 loops, best of 3: 288 ms per loop
user> (require '[clojure.core.reducers :as r])
nil
user> (defn c3 [p]
(with-open [rdr (clojure.java.io/reader p)]
(r/reduce + 0 (r/map len (line-seq rdr)))))
#'user/c3
user> (bench/quick-bench
(c3 "/tmp/test"))
WARNING: Final GC required 587.4416454410667 % of runtime
Evaluation count : 6 in 6 samples of 1 calls.
Execution time mean : 319.255797 ms
Execution time std-deviation : 74.168228 ms
Execution time lower quantile : 259.756586 ms ( 2.5%)
Execution time upper quantile : 436.680869 ms (97.5%)
Overhead used : 2.812543 ns
nil
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment