> This one is quite good for me.
> (defn countnl
>  [#^bytes buf]
>  (let [nl (int 10)]
>   (areduce buf idx count (int 0)
>            (if (== (int (aget buf idx)) nl)
>              (unchecked-inc count)
>              count))))
>
>
> It appears that == is not resolved for bytes. So converting to int works fine.

aha, converting to int works for me as well.  With bytes it triggers
the reflection warning and takes minutes to run.  with the int
conversion, it runs in an average of 28ms (just a little over java).
Here's something really strange: I de-inlined the newline int, so my
function looks like this:

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

On my machine, this is running in 19.6ms (after the warmup it's really
constant).  That's the exact speed as the java code, which is really
cool.  it actually works the same way if you just replace the inlined
"10" with "(int 10)", and it makes it just as fast as the explicit
looping code below.  Somehow on my earlier tests the let binding was
really slowing things down, but in this latest function it seems to
have no effect.  weird...

> In this situation, inlining (int 10) does not buy much.

interesting; for me replacing the 10 with (int 10) brings my times
from 28.7ms to 19.6ms.

> This one, though, is even faster and should be not far from the java
> one, if you want to try it.
>
> (defn countnl
>  [#^bytes buf]
>  (let [nl (int 10)
>        n (int (alength buf))]
>   (loop [idx (int n) count (int 0)]
>         (if (zero? idx)
>             count
>             (let [idx (unchecked-dec idx)]
>               (if (== (int (aget buf idx)) nl)
>                 (recur  idx (unchecked-inc count))
>                 (recur  idx count)))))))
>
> It would be interesting to know why it is nearly twice as fast as the
> areduce version on my computer.

That runs in 21.3ms for me, which actually makes it a tiny bit slower
than the code above (and slightly slower than the java code).

-- 
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