Lack of tail-recursion isn't the problem, boxing is. It's more of an issue with the JVM than it is with scala, and it's something that you'll run into with any form of functional programming on the platform (including e.g. google guava).
I keep hearing rumours that Oracle want to fix this because it's crippling anyone who wants to use the JVM for *big* science-style clustered number crunching, and it's the kind of revenue stream they don't want to miss out on. The fixnum proposal from John Rose is one possibility here: http://blogs.oracle.com/jrose/entry/fixnums_in_the_vm Having said that, tail-recursion certainly helps, and any recursive solution will neatly avoid the issues of boxing. For this reason, scala will automatically optimise any method that's capable of tail recursion, and even offers the @tailrec annotation so you can validate that the optimisation has indeed happened. Finally: There's a few scala solutions here: http://en.literateprograms.org/Fibonacci_numbers_(Scala) - including one tail-recursive design that's a great deal simpler and easier to understand than the imperative alternative. It should also run as fast, if not faster. On 9 February 2012 16:26, Casper Bang <[email protected]> wrote: > The Scala Fibonacci code performs 5-6 times worse than an imperative > iterative Java implementation: > > casper@workstation:~$ time java -cp /usr/share/java/scala-library.jar:. > ScalaFib > 4613732 > > real 0m0.243s > user 0m0.292s > sys 0m0.020s > > casper@workstation:~$ time java JavaFib > 4613732 > > real 0m0.052s > user 0m0.040s > sys 0m0.008s > > Frankly, that's a much higher penalty than I would've thought. Lack of > tail-recursion? (Scala 2.9, OpenJDK 1.6.0_23) > > Btw. this functional style has also been possible in C# since .NET 3.0 > (2006). > > -- You received this message because you are subscribed to the Google Groups "The Java Posse" group. To post to this group, send email to [email protected]. To unsubscribe from this group, send email to [email protected]. For more options, visit this group at http://groups.google.com/group/javaposse?hl=en.
