Clojureの怠惰系列対変換器ベンチマーク


this threadによると、私は、トランスデューサで怠惰なシーケンスだけを使用して比較しました.
I/Oファクタを追加するには、以下のプログラムを使って「偽. txt」というデータファイルを用意しました.
(with-open [w (io/writer "fake.txt")]
  (doseq [n (range 10000000)]
    (.write w (str n "\n"))))
F 1は怠惰なシーケンスベースのバージョンです.これは“偽. txtから”データを読み取り、計算のいくつかのステップを行います.
(defn f1
  []
  (with-open [r (io/reader "fake.txt")]
    (->> (line-seq r)
         (map parse-long)
         (map inc)
         (filter even?)
         (map inc)
         (reduce + 0))))
F 2は、F 1のトランスデューサベースのバージョンです.
(defn f2
  []
  (with-open [r (io/reader "fake.txt")]
    (transduce (comp (map parse-long)
                     (map inc)
                     (filter even?)
                     (map inc))
               +
               (line-seq r))))
私は評価基準を使って評価しました.
(with-progress-reporting (quick-bench (f1) :verbose))
(with-progress-reporting (quick-bench (f2) :verbose))
これがその結果です.
#################### F1 ###################
Evaluation count : 6 in 6 samples of 1 calls.
Execution time sample mean : 3.811858 sec
Execution time mean : 3.812064 sec

#################### F2 ###################
Evaluation count : 6 in 6 samples of 1 calls.
Execution time sample mean : 1.490624 sec
Execution time mean : 1.490777 sec
怠惰なシーケンスバージョンであるF 1は3.812064秒かかりました.変換器のバージョンであるF 2は1.490777を取った.したがって、変換器のバージョンは、遅延シーケンスバージョンよりも155.71 %高速です.
簡単に言えば、このバイアスされた実験は、変換器バージョンが純粋な怠惰シーケンスバージョンよりずっと速いことを示します.