Skip to content

Instantly share code, notes, and snippets.

@milesegan
Created June 24, 2010 22:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save milesegan/452095 to your computer and use it in GitHub Desktop.
Save milesegan/452095 to your computer and use it in GitHub Desktop.
;; 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