Ported this to scala (I know, I didn't do this functionally, this is a pretty straight port of the C example), ran in 4 seconds (of course, I've got about a bajillion other apps open right now, so take this with a grain of salt):
object Main {
def fib(n : int) : int = {
if(n < 2) n
else
fib(n-1) + fib(n-2)
}
def main(args: Array[String]) = {
for (i <- 0 until 37) {
printf("n = " + i.toString + " => "+fib(i).toString)
}
}
}
On my 1.4ghz core 2 duo laptop, which isn't a very powerful machine, the above scala code runs in about 1 second when compiled. If I let the code run once to to let the JIT warm up and only measure the execution of the main body, it runs in .60 seconds on average. By comparison, gcc with -O3 runs the program in about .57 seconds.
I don't know how you got 4 seconds unless you ran the program as a script and had a lot of other stuff running simultaneously. A simple scala hello world program on my machine has a startup time of 1.5 seconds when run as a script.
Comments
Ported this to scala (I know, I didn't do this functionally, this is a pretty straight port of the C example), ran in 4 seconds (of course, I've got about a bajillion other apps open right now, so take this with a grain of salt):
On my 1.4ghz core 2 duo laptop, which isn't a very powerful machine, the above scala code runs in about 1 second when compiled. If I let the code run once to to let the JIT warm up and only measure the execution of the main body, it runs in .60 seconds on average. By comparison, gcc with -O3 runs the program in about .57 seconds.
I don't know how you got 4 seconds unless you ran the program as a script and had a lot of other stuff running simultaneously. A simple scala hello world program on my machine has a startup time of 1.5 seconds when run as a script.