With type hints your implementation should run faster:

(defn fib ^long [^long n]
  (if (< n 2)
    n
    (+ (fib (- n 2)) (fib (- n 1)))))

What does your Java code look like? Is it also recursive?


On 13 August 2017 at 10:54, Daniel Gerlach <daniel.gerlach1...@gmail.com>
wrote:

> Hey,
>
> out of curiosity i did some benchmarking on my Macbook Pro 13 i5 2,7 GHz.
>
> I chose a simple naive fibonacci implementation as a candidate
> (i know that is not a good comparison value for real-world cases)
>
> The implementation looks like this:
>
> (defn fib [n]
>   (if (< n 2)
>     n
>     (+ (fib (- n 2)) (fib (- n 1)))))
>
>
>
> The results are a little bit surprising.
>
> The average time for fib(32) in Clojure was ~ 500ms
> The same algorithm in Java takes ~ 15 ms to finish.
> That means Clojure it's ~30x slower than Java for this special case.
>
> I also "warmed up" the JVM in both cases. For Clojure i used "criterium".
>
>
> Can somebody explain? Do i do something wrong? Are there any optimization
> <http://www.dict.cc/englisch-deutsch/optimization.html>s possible, e.g.
> type hints etc. ?
> Does Clojure has problems with recursive functions?
>
>
> Greetings
>
> Daniel Gerlach
>
> --
> 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
> ---
> You received this message because you are subscribed to the Google Groups
> "Clojure" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to clojure+unsubscr...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.
>



-- 
James Reeves
booleanknot.com

-- 
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
--- 
You received this message because you are subscribed to the Google Groups 
"Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to clojure+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to