Hi,

On Feb 23, 6:51 am, "Edward Z. Yang" <ezy...@mit.edu> wrote:

>     (ns maptest (:gen-class))
>
>     (defn mk-random-stream []
>         (let [r (new ec.util.MersenneTwisterFast)]
>                  (repeatedly (fn [] (. r (nextInt))))))
>
>     (defn -main [sn]
>       (let [n  (Integer/parseInt sn)
>                   rs (take (* 2 n) (mk-random-stream))]
>        (time (let [m  (apply hash-map (take (* n 2) (interleave rs rs)))]
>             (reduce
>                     (fn [s k] (let [v (get m k)]
>                                             (+ s (if (nil? v) 0 v))))
>                     0
>                     (take n (drop (/ n 2) rs)))))))
>
> I was careful to compile the Clojure code before attempting to run it.

This will not have any effect on runtime execution, because the
Clojure code is compiled anyway upon load. So this only effect startup-
time, which you don't measure.

> I also pre-allocate the entire random sequence to prevent it from
> muddying the execution time.

In fact you are not. You create a lazy sequence (repeatedly + take)
which is completely realised in the time call. However I don't know
about Haskell being lazy. So the same might happen there...

Without knowing much about benchmarking and the like, here my try on
the code:

(defn -main
  [n]
  (let [n       (Integer/parseInt n)
        n2      (* 2 n)
        r       (ec.util.MersenneTwisterFast.)
        rs      (take n2 (repeatedly #(.nextInt r)))
        dropped (doall (drop n rs))]
    (time
      (let [m (zipmap rs rs)]
        (reduce #(+ %1 (get m %2 0)) 0 dropped)))))

This should get rid of all laziness in the time'd execution and get as
close to the map operations as possible.

Usual disclaimers about "broken microbenchmarks", "not averaging",
"using a 'cold' JIT" etc. apply. I'm no expert here so I won't comment
on that. However I recommend investigating the presentations by Cliff
Click on these topics to avoid common pitfalls.

Sincerely
Meikel

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