Using some in-code timers, I got 20 ms for the Scala version and 0 ms
for a imperative Java version that I wrote

I agree that the runtime performance issues of Scala are quite real
and substantial. The Java/Scala runtime performance gap is larger than
the Java/C performance gap from my experience. Scala also has
compilation type performance issues, which are arguably less
important.

Also, sure you can do some functional/immutable programming in C#, but
that's really not the strength of C# or a good choice for more serious
functional/immutable programming. Even if you prefer pure Microsoft
branded tech, F# is far better for that.

You can also do functional/immutable style designs in Java. Not as
elegantly as you could in Scala/Haskell/Clojure/OCaml/F#, but my team
is doing exactly that.

FYI, here is the Java code that I used:

        public static void main(String[] args) {
                long start = System.currentTimeMillis();
                long sum = 0;

                long val1 = 1;
                long val2 = 2;

                while (val2 <= 4000000) {
                        if (val2 % 2 == 0) {
                                sum += val2;
                        }

                        long next = val1 + val2;
                        val1 = val2;
                        val2 = next;
                }

                long duration = System.currentTimeMillis() - start;
                System.out.println(String.format("sum = %d", sum));
                System.out.println(String.format("duration = %d", duration));
        }

And the Scala:

        val start = System.currentTimeMillis();

        def fib(x: Int, y: Int): Stream[Int] = {
                x #:: fib (y, x + y)
        }

        val answer = fib(1, 2).takeWhile(n => n <= 4000000).filter(n => n % 2
== 0).sum
        val duration = System.currentTimeMillis() - start;

        println(answer)
        println(duration);



On Feb 9, 10:26 am, 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.

Reply via email to