Hi All,

On Fri, Dec 24, 2010 at 4:32 PM, Meikel Brandmeyer <m...@kotka.de> wrote:

> Most interesting is also the relation between the different versions on the
> given machine. Just the numbers of one algorithm aren't really comparable, I
> guess. (different machine, different load, different phase of moon, who
> knows...)
>

Did the below just for my own amusement on a 64bit linux box, and thought it
might be interesting to others. The fastest implementation was using areduce
with 1.3.0-alpha and unchecked ops for 2800ns. Unoptimized C was doing 700ns
though, and with -O2 it was 400ns. Reminds me why JNI can be so valuable
sometimes.


/* -O0 low of 700ns
-02 low of 400ns */
int char_count(char* ptr)
{
  int count = 0;
  int i;

  for(i=0; ptr[i]; ptr++)
    if(ptr[i]!=32)
      count++;

  return count;
}


;; 1.2.0 low of 65000ns



;; 1.3.0-alpha3 low of 46000ns



;;



(defn count-num-chars-v1 [^String s]
  (loop [s s acc 0]
    (if (seq s)
      (recur (rest s) (if (= (first s) \space) acc (inc acc)))
      acc)))


;; 1.2.0 low of 6200ns



;; 1.3.0-alpha3 low of 4800ns



;;



(defn count-num-chars-v2 [^String s]
  (let [len (.length s)
        space (int 32)]
    (loop [i (int 0), c (int 0)]
      (if (< i len)
        (recur
         (inc i)
         (if (== (.codePointAt s i) space)
           c
           (unchecked-inc c)))
               c))))


;; 1.2.0 low of 12500ns



;; 1.3.0-alpha3 low of 4450ns



;; 1.3.0-alpha3 with *unchecked-math* enabled low of 2800ns



;;



(defn count-num-chars-v3 [^String s]
  (let [as (.toCharArray s)]
       (areduce as idx acc 0 (if (= (int (aget as idx)) (int \space)) acc
(inc acc)))))


;; 1.2.0 untested



;; 1.3.0-alpha3 low of 4500ns



;;



(defn count-num-chars-v4 ^long [^String s]
  (let [l (.length s)
        c \space]
    (loop [i 0 acc 0]
      (if (< i l)
        (recur (inc i)
               (if (identical? (.charAt s i) c) acc
                   (inc acc)))
        acc))))


Hope this helps,
    Alan

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