Created
June 24, 2010 22:42
-
-
Save milesegan/452095 to your computer and use it in GitHub Desktop.
This file contains 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
;; to create the test file: | |
;; java slurptest.clj makewords 100 | |
;; | |
;; to run the test | |
;; java -Xmx3G -Xms3G clojure.main slurptest.clj slurp|slurp2 | |
(import '[java.io BufferedReader InputStreamReader]) | |
(ns slurptest | |
(:require [clojure.java.io :as jio])) | |
(defn- normalize-slurp-opts | |
[opts] | |
(if (string? (first opts)) | |
(do | |
(println "WARNING: (slurp f enc) is deprecated, use (slurp f :encoding enc).") | |
[:encoding (first opts)]) | |
opts)) | |
(defn slurp2 | |
"Reads the file named by f using the encoding enc into a string and returns it." | |
{:added "1.0"} | |
([f & opts] | |
(let [opts (normalize-slurp-opts opts) | |
data (StringBuilder.) | |
buffer (char-array 4096)] | |
(with-open [#^java.io.Reader r (apply jio/reader f opts)] | |
(loop [c (.read r buffer)] | |
(if (neg? c) | |
(str data) | |
(do | |
(.append data buffer 0 c) | |
(recur (.read r buffer))))))))) | |
(case (first *command-line-args*) | |
"makewords" | |
(let [times (Integer. (nth *command-line-args* 1)) | |
data (slurp "/usr/share/dict/words")] | |
(with-open [f (java.io.FileWriter. "words")] | |
(doseq [i (range times)] | |
(.write f data)))) | |
"slurp2" | |
(time | |
(println (count (slurp2 "words")))) | |
"slurp" | |
(time | |
(println (count (slurp "words"))))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment