Hello,
Am 19.10.2008 um 17:56 schrieb Lauri Oherd:
There is also a faster way to calculate fibonacci numbers in Clojure
(code taken from from
http://en.wikibooks.org/wiki/Clojure_Programming#Lazy_Fibonacci):
(defn fib-seq []
((fn rfib [a b]
(lazy-cons a (rfib b (+ a b
0 1))
user=
This lazy cached calculate is wonderful ,but i think the benefit from
it mostly due to cache .
On Oct 19, 11:56 pm, "Lauri Oherd" <[EMAIL PROTECTED]> wrote:
> There is also a faster way to calculate fibonacci numbers in Clojure
> (code taken from
> fromhttp://en.wikibooks.org/wiki/Cloju
There is also a faster way to calculate fibonacci numbers in Clojure
(code taken from from
http://en.wikibooks.org/wiki/Clojure_Programming#Lazy_Fibonacci):
(defn fib-seq []
((fn rfib [a b]
(lazy-cons a (rfib b (+ a b
0 1))
user=> (time (take 38 (fib-seq)))
"Elapsed time: 0.0329
Scala is sure to use java primitive int type underline, i.e value
type and boxed to java Integer when necessarily
But why not Clojure auto make this ?
gerry
On Oct 19, 11:31 pm, "[EMAIL PROTECTED]" <[EMAIL PROTECTED]>
wrote:
> Here is coersion version for Clojure
>
> (defn fib [n]
> (l
Here is coersion version for Clojure
(defn fib [n]
(let [n (int n)]
(if (or (zero? n) (= n 1))
1
(+ (fib (dec n) ) (fib (- n 2))
(time (fib 36))
"Elapsed time 8848.865149"
not much better and how to type hint for a int type?
--~--~-~--~~
On Oct 19, 7:49 pm, "[EMAIL PROTECTED]" <[EMAIL PROTECTED]> wrote:
> Clojure's
>
> (defn fib [n]
>(if (or (zero? n) (= n 1))
>1
> (+ (fib (dec n) ) (fib (- n 2)
>
> (time (fib 36))
>
> "Elapsed Time: 10475.325226 msecs"
> 24157817
>
> Scala's
>
> def fib(n:Int):Int=if (
Just use type hint in Clojure version and you'll see quite a
difference in performance.
Your scala version is completely optimized / crippled to integers
(maybe even unboxed), so there is no dynamic runtime overhead.
IMHO, this kind of microbenchmarks are just good for finding general
weak point
Clojure's
(defn fib [n]
(if (or (zero? n) (= n 1))
1
(+ (fib (dec n) ) (fib (- n 2)
(time (fib 36))
"Elapsed Time: 10475.325226 msecs"
24157817
Scala's
def fib(n:Int):Int=if (n==0||n==1)1 else fib(n-1)+fib(n-2)
def time(cal: =>Int)={
val beginTime=System.currentTimeMi