Scala perf: Why is this Scala app 30x slower than the equivalent Java app?

后端 未结 3 1983
感情败类
感情败类 2021-02-07 11:33

I am a very proficient C# developer, but need to start writing code that works on the JVM. The Java language is feature poor compared to C# these days, so I was interested in t

3条回答
  •  执笔经年
    2021-02-07 11:50

    So, I guess I figured out the answer myself.

    The problem is in the call to System.nanoTime. Doing this has some initialization cost (loading up the Java base libraries, etc) which is much less expensive to load when called from the Java runtime than from the Scala runtime.

    I prove this by changing the initial value of total, instead setting it to

    var total: Long = System.nanoTime()
    

    This is added before the first "warm up" loop, and doing so now makes both versions of the app (Java and Scala) run at the same time: about 2100 for 1000000 iterations.

    Thanks for your guys' help on this, I wouldn't have figured this out without your assistance.

    ps: I'll leave the "accepted answer" as-is because I wouldn't have tracked this down without his help.

提交回复
热议问题