Scala perf:为什么这个Scala应用程序比同等的Java应用程序慢30倍? [英] Scala perf: Why is this Scala app 30x slower than the equivalent Java app?

查看:147
本文介绍了Scala perf:为什么这个Scala应用程序比同等的Java应用程序慢30倍?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一名非常熟练的C#开发人员,但需要开始编写适用于JVM的代码。与C#相比,Java语言功能较差,因此我对Scala提供的功能感兴趣。

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 the features that Scala offers.

然而,当听到Scala中的所有操作符都是方法时,我开始怀疑对数学繁重的计算会产生的性能影响(这对于我的团队编写的应用程序类型)

However, when hearing that in Scala, all operators are simply methods, I became suspicious of the performance impact that would have on math-heavy computations (which is important for the types of applications my team writes)

所以我运行了一些简单的基于int的测试,发现Scala比同等的Java代码慢大约30倍。不好!谁能告诉我我做错了什么?或者如何提高scala示例的计算性能以与Java相提并论?

So I ran some simple int based tests, and find that Scala is about 30x slower than the equivalent Java code. Not good! Can anyone tell me what I'm doing wrong? or how to improve the computational performance of the scala example to be on par with Java?

UPDATE1:正如前两个答案所指出的,我是一个超级noob并在IntelliJ IDE中运行它。我不知道如何通过java命令行运行scala应用程序,这可能是IntelliJ问题。感谢帮助人员,在继续执行perf测试之前,我需要调查scala的简单命令行执行,因为IDE给出的结果显然太不准确了。

UPDATE1: as pointed out by the first two answers, I was being a super-noob and running this in the IntelliJ IDE. I don't know how to run the scala app via the java command line, which may be an IntelliJ issue. Thanks for the help guys, I'll need to investigate simple commandline execution of scala before I continue with perf testing, as the IDE given results are obviously too inaccurate.

UPDATE2 :Luigi在评论中说他在IntelliJ中得到了相同的时间,所以看来我的差异不是因为IntelliJ?关于这可能是什么的任何其他想法?我将尝试通过命令行运行并使用我的结果发布更新。

UPDATE2: Luigi in the comments says in IntelliJ he gets equal times, so it seems that my wild difference isn't due to IntelliJ? Any other ideas on what this could be? I'll try getting this running via command line and post an update with my results.

UPDATE3:
通过命令行运行后,得到相同的30x性能差异。

我的电脑是3核AMD x64 3.4 Ghz,运行J2SE 6 jdk 64bit 1.6.0_31,Window7。

UPDATE3: after running this via commandline, I get the same 30x perf difference.
My computer is a 3core AMD x64 3.4Ghz, running J2SE 6 jdk 64bit 1.6.0_31, Window7.

这是我的运行时:
Java:210ms。

Scala:在2000到7400ms之间(通常是7000范围)

Here are my runtimes: Java: 210ms.
Scala: between 2000 and 7400ms (generally the 7000 range)

因此,我认为问题仍然存在。为什么scala在我的平台上运行得如此之慢?使用java 64bit运行时,还是使用Java 6?

so, i suppose the question is still open. why is scala running so slow on my platform? something with the java 64bit runtime, or with Java 6?

运行时版本:

C:\Users\jason>java -showversion
java version "1.6.0_31"
Java(TM) SE Runtime Environment (build 1.6.0_31-b05)
Java HotSpot(TM) 64-Bit Server VM (build 20.6-b01, mixed mode)

C:\Users\jason>scala
Welcome to Scala version 2.9.1-1 (Java HotSpot(TM) 64-Bit Server VM, Java 1.6.0_31).

更新4,而我的原始测试有30倍的差异,将迭代次数增加到100000000会导致差异缩小大约33%,所以似乎scala仍然被我的机器上一些未知的初始化成本所支配。我会以最高评级的答案结束这个,因为我认为我们不会发现性能问题,因为除了我看到这个问题之外没有人:(

UPDATE 4 while my original test has a 30x difference, increasing the iterations to 100000000 causes the difference to shrink to about 33%, so it seems scala was still being dominated by some unknown initialization cost on my machine. i'll close this with the highest rated answer as i don't think we'll find out the performance problem, due to no one except me seeing the issue :(

以下是我的示例应用:

//scala
object HelloWorld {
  //extends Application {
  def main(args: Array[String]) {
    println("hello scala")
    var total: Long = 0
    var i: Long = 0
    var x: Long=0;
    //warm up of the JVM to avoid timing of runtime initialization
    while (i < 100000)
    {
      x=i;
      x += x - 1;
      x -= x + 1;
      x += 1;
      x -= 1;
      total += x;
      i+=1;
    }
    //reset variables
    total = 0
    i = 0;
    //start timing
    var start: Long = System.nanoTime

    //run test
    while (i < 100000) {
      x=i;
      x += x - 1;
      x -= x + 1;
      x += 1;
      x -= 1;

      total += x;
      i+=1;
    }
    var end: Long = System.nanoTime
    System.out.println("ms, checksum = ")
    System.out.println((end - start) / 1000)
    System.out.println(total)
  }
}

这里是java等价物,快30倍

and here is the java equivalent, 30x faster

//java
public class app {
    public static void main(String[] args)
    {
        String message = "hello, java";
        System.out.println(message);
        long total = 0;
        //warm up of the JVM to avoid timing of runtime initialization
        for(long i=0;i< 100000;i++)
        {
            long x=i;
            x+=x-1;
            x-=x+1;
            x++;
            x--;
            total+=x;
        }
        //reset variables
        total = 0;
        //start timing and run test
        long start = System.nanoTime();
        for(long i=0;i< 100000;i++)
        {
            long x=i;
            x+=x-1;
            x-=x+1;
            x++;
            x--;
            total+=x;
        }
        long end = System.nanoTime();
        System.out.println("ms, checksum = ");
        System.out.println((end-start)/1000);
        System.out.println(total);
    }
}


推荐答案

所以,我想我自己想出了答案。

So, I guess I figured out the answer myself.

问题在于对 System.nanoTime 的调用。这样做有一些初始化成本(加载Java基础库等),从Java运行时调用时加载比从Scala运行时加载要便宜得多。

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.

我通过更改total的初始值来证明这一点,而不是将其设置为

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

var total: Long = System.nanoTime()

这是在第一个热身循环之前添加的,现在这样做会使这两个版本的应用程序(Java和Scala)同时运行:大约2100次1000000次迭代。

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:我会按原样留下已接受的答案,因为如果没有他的帮助,我不会跟踪这个问题。

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

这篇关于Scala perf:为什么这个Scala应用程序比同等的Java应用程序慢30倍?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆