Hi,
On Jul 15, 1:51 pm, Dragan <[email protected]> wrote:
> [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]
Comparing such a loop with reduce is a bit unfair,
since the loop iteration heavily dominates the
execution. reduce gives you much more convenience
to handle complex data structures, which - I think -
a long is not.
For such tight loops, you want something like this:
(loop [sum (long 0)
i (long 1)]
(if (< i 100000)
(recur (+ sum i) (inc i))
sum))
This should give you higher performance.
> 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?
There is no difference of code run at the Repl and code,
which is compiled (which is not equivalent with gen-class btw.).
Clojure code always gets compiled before it is executed.
Of course there is a slight compilation overhead at the Repl.
Sincerely
Meikel
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to [email protected]
Note that posts from new members are moderated - please be patient with your
first post.
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
-~----------~----~----~----~------~----~------~--~---