> Replace also  (unchecked-add count 1) with  (unchecked-add count (int 1))
>
> (this should get easier in 1.3)

That didn't change anything for my tests, but this code:

(defn countnl
  [#^bytes buf]
  (areduce buf idx count (int 0)
           (if (= (aget buf idx) 10)
             (unchecked-add count 1)
             count)))

does 34ms runs (the .java code does 20ms runs on the same machine).
The biggest things that make a difference are replacing (inc count)
with (unchecked-add count 1), initializing count to (int 0) rather
than 0, and getting rid of the (let [nl (byte 0)]...).  I was a bit
disappointed that the let statement was so expensive, since I really
prefer seeing a variable to a magic number.  Has somebody written a
macro that can be used to rewrite code replacing variable references
with their constant values?  It would be of limited use (constants
only), but it would make the code prettier :)  For posterity, here are
some variations on countnl and their timings on my machine:

all runs are invoked with this command: "java -server -cp clojure.jar
clojure.main iterate.clj"; clojure.jar is from the clojure-1.2.zip
distribution


(defn countnl
  [#^bytes buf]
  (let [nl (byte 10)]
    (areduce buf idx count 0
             (if (= (aget buf idx) nl)
               (inc count)
               count))))

Wanted 16777216 got 16777216 bytes
"Elapsed time: 184.172834 msecs"
Got 65875 nls
Wanted 16777216 got 16777216 bytes
"Elapsed time: 193.546018 msecs"
Got 64995 nls
Wanted 16777216 got 16777216 bytes
"Elapsed time: 142.546453 msecs"
Got 65677 nls
Wanted 16777216 got 16777216 bytes
"Elapsed time: 135.843933 msecs"
Got 66117 nls
Wanted 16777216 got 16777216 bytes
"Elapsed time: 134.882156 msecs"
Got 65449 nls
Wanted 16777216 got 16777216 bytes
"Elapsed time: 134.864881 msecs"
Got 65393 nls
Wanted 16777216 got 16777216 bytes
"Elapsed time: 134.854118 msecs"
Got 65065 nls
Wanted 16777216 got 16777216 bytes
"Elapsed time: 134.82848 msecs"
Got 65346 nls
Wanted 16777216 got 16777216 bytes
"Elapsed time: 134.803702 msecs"
Got 65783 nls
Wanted 16777216 got 16777216 bytes
"Elapsed time: 134.832489 msecs"
Got 65566 nls

(defn countnl
  [#^bytes buf]
  (let [nl (byte 10)]
    (areduce buf idx count (int 0)
             (if (= (aget buf idx) nl)
               (inc count)
               count))))

Wanted 16777216 got 16777216 bytes
"Elapsed time: 238.409697 msecs"
Got 65698 nls
Wanted 16777216 got 16777216 bytes
"Elapsed time: 208.872818 msecs"
Got 65381 nls
Wanted 16777216 got 16777216 bytes
"Elapsed time: 88.786986 msecs"
Got 65358 nls
Wanted 16777216 got 16777216 bytes
"Elapsed time: 88.76816 msecs"
Got 65686 nls
Wanted 16777216 got 16777216 bytes
"Elapsed time: 88.899431 msecs"
Got 65683 nls
Wanted 16777216 got 16777216 bytes
"Elapsed time: 88.783275 msecs"
Got 65491 nls
Wanted 16777216 got 16777216 bytes
"Elapsed time: 88.735416 msecs"
Got 65749 nls
Wanted 16777216 got 16777216 bytes
"Elapsed time: 88.762987 msecs"
Got 65837 nls
Wanted 16777216 got 16777216 bytes
"Elapsed time: 88.749591 msecs"
Got 65759 nls
Wanted 16777216 got 16777216 bytes
"Elapsed time: 88.72971 msecs"
Got 65366 nls

(defn countnl
  [#^bytes buf]
  (areduce buf idx count (int 0)
           (if (= (aget buf idx) 10)
             (inc count)
             count)))

Wanted 16777216 got 16777216 bytes
"Elapsed time: 185.465613 msecs"
Got 65344 nls
Wanted 16777216 got 16777216 bytes
"Elapsed time: 186.856418 msecs"
Got 65529 nls
Wanted 16777216 got 16777216 bytes
"Elapsed time: 73.778183 msecs"
Got 65662 nls
Wanted 16777216 got 16777216 bytes
"Elapsed time: 73.675509 msecs"
Got 65034 nls
Wanted 16777216 got 16777216 bytes
"Elapsed time: 73.674777 msecs"
Got 65459 nls
Wanted 16777216 got 16777216 bytes
"Elapsed time: 73.630553 msecs"
Got 65172 nls
Wanted 16777216 got 16777216 bytes
"Elapsed time: 73.668685 msecs"
Got 64939 nls
Wanted 16777216 got 16777216 bytes
"Elapsed time: 73.74801 msecs"
Got 65462 nls
Wanted 16777216 got 16777216 bytes
"Elapsed time: 73.699187 msecs"
Got 65602 nls
Wanted 16777216 got 16777216 bytes
"Elapsed time: 73.611555 msecs"
Got 64937 nls

(defn countnl
  [#^bytes buf]
  (areduce buf idx count (int 0)
           (if (= (aget buf idx) 10)
             (unchecked-add count 1)
             count)))

Wanted 16777216 got 16777216 bytes
"Elapsed time: 82.865761 msecs"
Got 65266 nls
Wanted 16777216 got 16777216 bytes
"Elapsed time: 34.228646 msecs"
Got 65522 nls
Wanted 16777216 got 16777216 bytes
"Elapsed time: 33.989741 msecs"
Got 65537 nls
Wanted 16777216 got 16777216 bytes
"Elapsed time: 33.947045 msecs"
Got 65688 nls
Wanted 16777216 got 16777216 bytes
"Elapsed time: 33.941481 msecs"
Got 65395 nls
Wanted 16777216 got 16777216 bytes
"Elapsed time: 33.952149 msecs"
Got 65822 nls
Wanted 16777216 got 16777216 bytes
"Elapsed time: 33.972258 msecs"
Got 65495 nls
Wanted 16777216 got 16777216 bytes
"Elapsed time: 33.903989 msecs"
Got 65244 nls
Wanted 16777216 got 16777216 bytes
"Elapsed time: 33.927276 msecs"
Got 65331 nls
Wanted 16777216 got 16777216 bytes
"Elapsed time: 33.917789 msecs"
Got 65470 nls

(defn countnl
  [#^bytes buf]
  (areduce buf idx count (int 0)
           (if (= (aget buf idx) 10)
             (unchecked-add count (int 1))
             count)))

Wanted 16777216 got 16777216 bytes
"Elapsed time: 123.2343 msecs"
Got 65400 nls
Wanted 16777216 got 16777216 bytes
"Elapsed time: 34.117048 msecs"
Got 65602 nls
Wanted 16777216 got 16777216 bytes
"Elapsed time: 33.86488 msecs"
Got 65870 nls
Wanted 16777216 got 16777216 bytes
"Elapsed time: 33.782857 msecs"
Got 65257 nls
Wanted 16777216 got 16777216 bytes
"Elapsed time: 33.80848 msecs"
Got 65861 nls
Wanted 16777216 got 16777216 bytes
"Elapsed time: 33.940678 msecs"
Got 65758 nls
Wanted 16777216 got 16777216 bytes
"Elapsed time: 33.950256 msecs"
Got 65683 nls
Wanted 16777216 got 16777216 bytes
"Elapsed time: 33.94562 msecs"
Got 65070 nls
Wanted 16777216 got 16777216 bytes
"Elapsed time: 33.947251 msecs"
Got 65424 nls
Wanted 16777216 got 16777216 bytes
"Elapsed time: 33.932402 msecs"
Got 65316 nls

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en

Reply via email to