Mercurial > rlm
view src/rlm/iterate.clj @ 0:78a630e650d2
initial import
author | Robert McIntyre <rlm@mit.edu> |
---|---|
date | Tue, 18 Oct 2011 00:57:08 -0700 |
parents | |
children |
line wrap: on
line source
1 (ns rlm.iterate2 "from the clojure mailing list"3 {:author "tsuraan"}4 (:gen-class5 :name "iterate"6 :main true7 ))10 (set! *warn-on-reflection* true)11 (import java.io.FileInputStream)13 (def *numbytes* (* 16 1024 1024))16 (defmacro better-areduce [a idx ret init expr]17 `(let [a# ~a18 alength# (alength a#)]19 (loop [~idx (int 0) ~ret ~init]20 (if (< ~idx alength#)21 (recur (unchecked-inc ~idx) ~expr)22 ~ret))))24 (defn translated-countnl25 [#^bytes buf]26 (let [total-length (alength buf)]27 (loop [count (int 0) current-index (int 0)]28 (if (>= current-index total-length)29 count30 (let [new-count31 (if (= (aget #^bytes buf current-index) (byte 10))32 (unchecked-inc count)33 count)]35 (recur new-count (unchecked-inc current-index)))))))37 (defn countnl-classic38 [#^bytes buf]39 (let [nl (byte 10)]40 (areduce buf idx count 041 (if (= (aget buf idx) nl)42 (inc count)43 count))))46 (defn countnl-lite47 "this is the absolute fastest function I could come up with.48 still 2.25 times slower that the java version."49 [#^bytes buf]50 (areduce buf idx count (int 0)51 (if (= (clojure.lang.RT/aget buf idx) 10)52 (unchecked-add count 1)53 count)))56 (defn #^Boolean is-ten? [#^Integer A]57 (= A 10))59 (defn countnl-max-power60 [#^bytes buf]61 (areduce buf idx count (int 0)62 (if (is-ten? (clojure.lang.RT/aget buf idx))63 (unchecked-inc count)64 count)))76 "(times are averages with 10 rounds)77 no-modifications78 classic: 198.15179 lite: 186.66881 unchecked-inc82 classic: 177.93883 lite: 129.60485 inlined-let86 classic: 202.60487 lite: 195.35789 make nl a constant90 classic:91 lite: 361.73493 make nl a constant with type hint94 lite: 394.4596 literal 10, no let97 lite: 102.87699 (int 10) instead of literal 10100 lite: 194.904102 (unchecked-add count 0) instead of count103 lite: 209.186105 direct reference to RT/aget, bypassing int cast106 lite: 99.767108 (int 0) instead of 0 initialization of count109 lite: 66.446111 make nl an (int 10) instead of (byte 10)112 lite: 180.003113 "119 (defmacro time-multi120 "Evaluates expr and prints the time it took. Returns the value of121 expr."122 {:added "1.0"}123 [expr]124 `(let [start# (. System (nanoTime))125 ret# ~expr126 end# (/ (double (- (. System (nanoTime)) start#)) 1000000.0)]127 (prn (str "Elapsed time: " end# " msecs"))128 [ret# end#]))132 (defn run [countnl-impl]133 (let [ifs (FileInputStream. "/dev/urandom")134 buf (make-array Byte/TYPE *numbytes*)135 runs 10]136 (loop [n 0 total-time 0]137 (if (= n runs) (/ total-time runs)138 (let [sz (.read #^FileInputStream ifs buf)]139 (println "Wanted" *numbytes* "got" sz "bytes")140 (let [[count time] (time-multi (countnl-impl buf))]141 (println "Got" count "nls")142 (recur (inc n) (+ total-time time))))))))144 (defn -main [& ignore]145 (run countnl-lite)146 (run countnl-classic)147 )