Last active
December 19, 2015 14:19
-
-
Save fbmnds/5968670 to your computer and use it in GitHub Desktop.
Explicite iteration
This file contains hidden or 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
| (require '[clojure.java.io :as io]) | |
| (def in-file "/path/to/datafile-4gb.tsv") | |
| (def out-file "/path/to/datafile-16gb.tsv") | |
| (defn multiply-string [s n] | |
| (cond | |
| (> n 1) (str s \newline (multiply-string s (dec n))) | |
| (= n 1) (str s \newline) | |
| :else nil)) | |
| (defn multiply-file [in out n] | |
| (with-open [reader (io/reader in) | |
| writer (io/writer out)] | |
| (loop [line (.readLine reader)] | |
| (if (nil? line) | |
| nil | |
| (do | |
| (.write writer (multiply-string line n)) | |
| (recur (.readLine reader))))))) | |
| user> (time (multiply-file in-file out-file 4)) | |
| "Elapsed time: 612539.345864 msecs" | |
| nil | |
| user> (/ 612539.345864 812899000) | |
| 7.535245410118601E-4 | |
| user> (def in-file "/path/to/datafile-16gb.tsv") | |
| #'user/in-file | |
| user> (def out-file "/path/to/datafile-64gb.tsv") | |
| #'user/out-file | |
| user> (time (multiply-file in-file out-file 4)) | |
| "Elapsed time: 2476705.02482 msecs" | |
| nil | |
| user> | |
| user> (/ 2476705.02482 (* 812990000 4)) | |
| 7.616037788964194E-4 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment