Hi, I've been playing with nio lately and it's been quite fun to delve in lower level stuff again, didn't do that in a long time. I've translated a small example, just to read a file, and the result is performing really well. Here's the code:
(defn read-file [#^String f #^String cs #^Integer buffer-size] (with-open [cin (. (new FileInputStream f) getChannel)] (let [buffer (. ByteBuffer (allocate buffer-size)) char-buffer (. ByteBuffer (allocate buffer-size)) charset (. Charset (forName cs)) decoder (. charset (newDecoder)) sb (new StringBuilder) read #(. cin (read buffer))] (loop [r (read)] (if (= r -1) (str sb) (do (. buffer flip) (. sb (append (. decoder (decode buffer)))) (. buffer clear) (recur (read)))))))) And a couple of micro-benchmarks in Windows XP SP3: nio=> (time (dotimes [n 1000] (read-file ".emacs" "ISO-8859-1" 8192))) "Elapsed time: 284.693456 msecs" nio=> (time (dotimes [n 1000] (read-file ".emacs" "ISO-8859-1" 8192))) "Elapsed time: 280.79918 msecs" nio=> (time (dotimes [n 1000] (read-file ".emacs" "ISO-8859-1" 8192))) "Elapsed time: 284.62619 msecs" nio=> (time (dotimes [n 1000] (slurp ".emacs"))) "Elapsed time: 530.622305 msecs" nio=> (time (dotimes [n 1000] (slurp ".emacs"))) "Elapsed time: 531.319221 msecs" nio=> (time (dotimes [n 1000] (slurp ".emacs"))) "Elapsed time: 527.439793 msecs" And in Debian Linux 5.0: user=> (time (dotimes [n 1000] (nio/read-file ".emacs" "ISO-8859-1" 8192))) "Elapsed time: 243.364117 msecs" user=> (time (dotimes [n 1000] (nio/read-file ".emacs" "ISO-8859-1" 8192))) "Elapsed time: 247.718237 msecs" user=> (time (dotimes [n 1000] (nio/read-file ".emacs" "ISO-8859-1" 8192))) "Elapsed time: 246.027863 msecs" user=> (time (dotimes [n 1000] (slurp ".emacs"))) "Elapsed time: 320.073016 msecs" user=> (time (dotimes [n 1000] (slurp ".emacs"))) "Elapsed time: 309.658266 msecs" user=> (time (dotimes [n 1000] (slurp ".emacs"))) "Elapsed time: 318.926749 msecs" Things to note: * Playing with buffer size obviously changes the results, still the nio version always outperformed slurp in my tests. The smallest buffer size I tried was 1024 bytes. * The Debian machine is a virtual one running under Windows. * Windows java is Sun's JDK version 1.6.0_06. * Linux java is OpenJDK version 1.6.0_0. - budu --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Clojure" group. To post to this group, send email to clojure@googlegroups.com Note that posts from new members are moderated - please be patient with your first post. To unsubscribe from this group, send email to clojure+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/clojure?hl=en -~----------~----~----~----~------~----~------~--~---