Skip to content

Instantly share code, notes, and snippets.

@fbmnds
Last active December 19, 2015 07:49
Show Gist options
  • Select an option

  • Save fbmnds/5920974 to your computer and use it in GitHub Desktop.

Select an option

Save fbmnds/5920974 to your computer and use it in GitHub Desktop.
;; benchmark 1
;;
user> (time (count in-seq))
"Elapsed time: 2225 msecs"
812990
;;
;; memoizing
;;
user> (time (count in-seq))
"Elapsed time: 208 msecs"
812990
user> (time (count in-seq))
"Elapsed time: 213 msecs"
812990
;;
user> (/ 2225 812990)
0.00273681103088599
;; benchmark 2, CLR version
;;
;; from Clojure Data Analysis Cookbook
;; the payload function - here freq. - could be trivially factored out,
;; a let binding in lazy-read-ok could keep a global state for it.
;;
(defn lazy-read-ok
"This one's OK. It handles everything lazy, but the lazy read is emeshed with
the logic.
This contrived example pulls the age out of each file.
Note that you still need to force everything to be read. Here I'm using
frequencies to do this."
([csv-file]
(with-open [in-file (io/text-reader csv-file)]
(frequencies (map #(nth % 2) (csv/read-csv in-file :separator \tab))))))
user=> (time (lazy-read-ok in-file))
"Elapsed time: 99062 msecs"
{"2001-02-12" 1640, "2001-03-02" 1646, "2001-01-22" 1639, "2000-03-03" 1558, "2001-12-13" 1731, "2001-04-03" 1653, "2000-06-22" 1568, "2001-08-29" 1704, "2001-11-23" 1720, "2000-12-12" 1637, ...}
user=> (/ 99062.0 812990)
0.12184897723219228
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment