Hidden performance cost in Scala?

后端 未结 4 1620
傲寒
傲寒 2020-12-12 11:11

I came across this old question and did the following experiment with scala 2.10.3.

I rewrote the Scala version to use explicit tail recursion:

impor         


        
4条回答
  •  失恋的感觉
    2020-12-12 12:05

    To make the Java version completely equivalent to your Scala code you need to change it like this.

    private int t = 20;
    
    
    private int t() {
        return this.t;
    }
    
    private void run() {
        int i = 10;
        while (!isEvenlyDivisible(2, i, t()))
            i += 2;
        System.out.println(i);
    }
    

    It is slower because the JVM can not optimize the method calls.

提交回复
热议问题