If you make it into a function and use type hints, that should help:

(defn sum [n]
  (let [n (int n)]
    (loop [ret (long 0) i (int 1)]
      (if (< i n)
        (recur (+ ret i) (inc i))
        ret))))

user=> (time (reduce + (range 1 1000000)))
"Elapsed time: 116.959837 msecs"
499999500000

user=> (time (sum 1000000))
"Elapsed time: 6.151341 msecs"
499999500000

Frantisek


On Jul 15, 1:51 pm, Dragan <draga...@gmail.com> wrote:
> Hi,
>
> I am trying to compare the performance of java loops to clojure reduce
> function. Noting special, Since I am just learning.
> Java code is something like:
>
> [code]
> long sum = 0;
> for (int i = 1; i < 1000000; i++ ){
>     sum = sum + i;}
>
> [/code]
>
> while in Clojure I used:
>
> [code]
> (reduce + (range 1 i))
> [/code]
>
> Execution time for the Java version is 7 ms, while Clojure needs 60 -
> 160 ms.
> Now, that is expected, since Clojure runs in REPL.
> Next, I tried the gen-class of the Clojure, and called that Class from
> Java (so no REPL involved), but the code haven't speed up at all!
> Am I missing something or such 10x performance penalty is usual for
> such operations?
--~--~---------~--~----~------------~-------~--~----~
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