Skip to content

Instantly share code, notes, and snippets.

@jonase
Created September 8, 2011 10:44
Show Gist options
  • Save jonase/1203122 to your computer and use it in GitHub Desktop.
Save jonase/1203122 to your computer and use it in GitHub Desktop.
(ns csvtest.core
(:require [clojure.java.io :as io]
[clojure.data.csv]
[clojure-csv.core]))
(def characters
(vec
(concat "0123456789"
"abcdefghijklmnopqrstuvxyz"
"ABCDEFGHIJKLMNOPQRSTUVXYZ"
"\"\n, ")))
(defn rand-char []
(rand-nth characters))
(defn rand-string
"Generates a random string of length max length n"
[n]
(apply str (repeatedly (rand-int n) rand-char)))
(defn csv-row
[n max-str-len]
(repeatedly n #(rand-string max-str-len)))
(defn generate-csv [file rows cols str-len]
(with-open [w (io/writer file)]
(clojure.data.csv/write-csv w (repeatedly rows #(csv-row cols str-len)))))
(comment
(generate-csv "resources/data.csv" 1e5 100 20)) ;; 109Mb csv file
(defn time-data-csv []
(time
(with-open [reader (io/reader "resources/data.csv")]
(doall (clojure.data.csv/read-csv reader))
:ok)))
(comment
(time-data-csv)) ;"Elapsed time: 12615.345416 msecs"
(defn time-clojure-csv []
(time
(with-open [reader (io/reader "resources/data.csv")]
(doall (clojure-csv.core/parse-csv
(clojure-csv.core/char-seq reader)))
:ok)))
(comment
(time-clojure-csv)) ;"Elapsed time: 106432.368293 msecs"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment