From 265c997ffa2975c7f6107a07cf22af2cae862489 Mon Sep 17 00:00:00 2001 From: Joel Holdbrooks Date: Thu, 1 Aug 2013 16:51:55 -0700 Subject: [PATCH] Add benchmarks to README --- README.md | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) diff --git a/README.md b/README.md index 4779eac..c98fc5d 100644 --- a/README.md +++ b/README.md @@ -70,3 +70,54 @@ true You can view the full expression [here](https://gist.github.com/noprompt/6106573/raw/fcb683834bb2e171618ca91bf0b234014b5b957d/word-re.clj) (it's approximately `1.5M`!). + +## Benchmarks + +```clojure +(use 'criterium.core) + +(def words + (-> (io/file "/usr/share/dict/words") + io/reader + line-seq)) + +(defn naive-pattern + "Create a naive regular expression pattern for matching every string + in strs." + [strs] + (->> strs + (clojure.string/join "|") + (format "(?:%s)"))) + +;; Shuffle 10000 words and build a naive and frak pattern from them. +(def ws (shuffle (take 10000 words))) +(def n-pat (naive-pattern ws)) +(def f-pat (frak/pattern ws)) + +;; Verify the naive pattern matches everything it was constructed from. +(every? #(re-matches n-pat %) ws) +;; => true + +;; Shuffle the words again since the naive pattern is built in the +;; same order as it's inputs. +(def ws' (shuffle ws)) + +;;;; Benchmarks + +;; Naive pattern + +(bench (doseq [w ws'] (re-matches n-pat w))) + +;; Execution time mean : 1.499489 sec +;; Execution time std-deviation : 181.365166 ms +;; Execution time lower quantile : 1.337817 sec ( 2.5%) +;; Execution time upper quantile : 1.828733 sec (97.5%) + +;; frak pattern + +(bench (doseq [w ws'] (re-matches f-pat w))) +;; Execution time mean : 155.515855 ms +;; Execution time std-deviation : 5.663346 ms +;; Execution time lower quantile : 148.168855 ms ( 2.5%) +;; Execution time upper quantile : 164.164294 ms (97.5%) +``` -- 2.25.1