Hmm, I should have thought of that.

New Clojure:

(ns hello.test
  (import org.jbox2d.common.Vec2)
  (:gen-class))

(defn -main [& args]
  (dotimes [q 5]
    (let [#^Vec2 a (Vec2. 1 2)
          #^Vec2 b (Vec2. 3 4)]
      (time (loop [x (int 0)]
              (when (< x (int 1e9))
                (.addLocal a b)
                (recur (unchecked-inc x))))))))

Old Clojure Output:
"Elapsed time: 11437.687558 msecs"
"Elapsed time: 10863.527399 msecs"
"Elapsed time: 11863.44317 msecs"
"Elapsed time: 11861.23116 msecs"
"Elapsed time: 11870.92791 msecs"
New Clojure Output:
"Elapsed time: 2492.033705 msecs"
"Elapsed time: 1489.316185 msecs"
"Elapsed time: 1484.606724 msecs"
"Elapsed time: 1480.983085 msecs"
"Elapsed time: 1486.435446 msecs"

This is the result of the Java code I posted,

Java Output:
2.263
4.471
4.484
4.452
4.409

I thought it was strange that the first iteration ran so fast, so I
changed 1e9 to (int)1e9 and the speed got better, but I still wonder
why the first iteration before was so fast, the JIT is supposed to
make it faster isn't it?

Old Java Output:
1.513
1.495
1.96
1.47
1.475

On Mar 10, 9:04 pm, David Nolen <dnolen.li...@gmail.com> wrote:
> On Thu, Mar 10, 2011 at 10:39 PM, Jarl Haggerty <jarlhagge...@gmail.com>wrote:
>
> > I've been benchmarking java and clojure programs and wanted to make
> > sure I was doing this right.  I made two fairly similar programs that
> > manipulated Vec2 objects from the JBox2D library.  At first clojure
> > was performing pretty poorly, then I tried compiling my clojure
> > script, and then replacing the inner doseq with a loop.  But java is
> > still 5x faster and I hear that clojure should be able to run as fast
> > as java and I wondered if there's any options in clojure I'm not using
>
> Try replacing doseq w/ dotimes. You need to cast 1e9 to int, just like you
> do w/ 0 if you're using Clojure <= 1.2.0. How long is it taking for you in
> Clojure and Java?
>
> David

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