Eclipse中的Scala执行时间 [英] Scala execution times in Eclipse

查看:230
本文介绍了Eclipse中的Scala执行时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我从Eclipse运行Scala程序时,有一些腥味。我运行一个App对象,运行7.8秒(实际执行时间与对象中的System.nanoTime计时)。当我从命令行运行相同的.class文件时,需要2.5秒。



我在控制台窗口上方看到它说

 < terminated> Run(1)[Scala Application] C:\Program Files\Java\jre6\bin\javaw.exe 

我想知道这是否与时俱进?另外,这是我的eclipse.ini设置,我根据Scala IDE页面中的建议设置:

  -vm 
C:\Program Files\Java\jre6\bin
-startup
plugins / org.eclipse.equinox.launcher_1.1.0.v20100507.jar
--launcher .library
plugins / org.eclipse.equinox.launcher.win32.win32.x86_64_1.1.1.R36x_v20100810
-product
org.eclipse.epp.package.java.product
--launcher.defaultAction
openFile
--launcher.XXMaxPermSize
256M
-showsplash
org.eclipse.platform
--launcher.XXMaxPermSize
256m
--launcher.defaultAction
openFile
-vmargs
-Dosgi.requiredJavaVersion = 1.5
-Xms256m
-Xmx2048m
- XX:PermSize = 64m
-Xss1M
-server
-XX:+ DoEscapeAnalysis
-XX:+ UseConcMarkSweepGC
-XX:+ UseCompressedOops

我有99%的确定这个相同的代码在几天前在Eclipse中运行了0.7秒。我已经尝试将代码粘贴到一个新项目并从那里运行,但并不会改变运行时间。该项目使用全局Scala编译器设置,所有这些都设置为默认值。



更新:我刚刚尝试在2.9.0.1 Scala库中进行交换,以便它使用与命令行完全相同的版本,运行时间没有任何差异。



我还尝试从命令行运行文件,上面带有所有-vmargs选项,没有任何区别。



任何想法发生了什么,还是需要检查的任何设置?谢谢。






更新2 :我现在已经半点回答了问题 - 我以不同的方式运行代码;但是这里的问题是:



版本1,扩展App:

  object P005_V2 extends App {
def isDivis(x:Int,i:Int):Boolean = {
if(i> 20)true
else if x%i!= 0)false
else isDivis(x,i + 1)
}
def find(n:Int):Int = if(isDivis(n,2))n else find(n + 2)

val t = System.nanoTime;
println(find(2))
println(System.nanoTime - t)
}

在Eclipse中需要0.7秒,2.3秒从命令提示符



版本2,从另一个对象实例

 对象P005_V2 {
def isDivis(x:Int,i:Int):Boolean = {
if(i > 20)true
else if(x%i!= 0)false
else isDivis(x,i + 1)
}
def find(n:Int) Int = if(isDivis(n,2))n else find(n + 2)

val t = System.nanoTime;
println(find(2))
println(System.nanoTime - t)
}

对象运行扩展App {
P005_V2

在Eclipse中7.6秒,从命令行开始2.5秒



所以,我知道应用程序的设计是为了使您能够在构造函数中执行某些操作,并且它们将被优化,与不推荐使用的应用程序不同。这里似乎发生的是因为我从构造函数调用 println(find(2)),不扩展的版本应用程序将不会被优化,并且需要更长时间。



但问题依然存在,为什么通过Eclipse和命令行执行之间巨大的速度差异?



确实,命令行版本运行缓慢,因为命令行中的Java版本需要0.7秒,Scala版本应该是一样快,如在Eclipse中运行的版本1所示)。

解决方案

经过一些实验,我有95%的答案,所以我会给它自己:



我正在运行Windows 7 64位。



命令行正在使用 32位JDK 环境,由JAVA_HOME变量指定。



Eclipse正在使用 64位JRE 环境通过窗口|偏好| Java |安装的JRE 和项目的系统库。



将其中的每一个更改为使用其他版本产生类似的运行时间。



java命令行将使用64位JVM,不管JAVA_HOME中指定什么,不同于$ code> scala ,这可能会导致Scala / Java比较中的混乱。



上述第1和第2版之间的运行时间差异是因为如上所述,版本2在对象的构造函数中运行昂贵的计算(而不是在版本1中使用 App )时的延迟的方法。 版本2非常错误,因为您不应该在构造函数中执行昂贵的计算和I / O。如果您直接从运行对象调用查找方法,则速度异常消失。



结论:



运行时间取决于您是运行32位还是64位JVM,这是在Eclipse中指定的,无论您的环境设置如何。如果使用64位版本,则需要特别小心,不要在构造函数中进行大量处理。



运行时间(秒):

  Java Scala Scala  - 构造函数
JRE 64位JDK 32位JRE 64位JDK 32位JRE 64位JDK 32位bit
Windows 7 64位0.7 2.4 0.7 2.5 7.6 2.5
Windows XP 32位不适用13.4不适用14不适用13.1
(较慢的机器)


There's something fishy going on when I run Scala programs from Eclipse. I run an App object and it takes 7.8 s to run (actual execution time timed with System.nanoTime in the object). When I run the same .class file from the command line it takes 2.5 s.

I notice above the Console window it says

<terminated> Run(1)[Scala Application] C:\Program Files\Java\jre6\bin\javaw.exe

I wonder if this has any bearing on the times. Also, here are my eclipse.ini settings, which I set according to the recommendations in the Scala IDE pages:

-vm
C:\Program Files\Java\jre6\bin
-startup
plugins/org.eclipse.equinox.launcher_1.1.0.v20100507.jar
--launcher.library
plugins/org.eclipse.equinox.launcher.win32.win32.x86_64_1.1.1.R36x_v20100810
-product
org.eclipse.epp.package.java.product
--launcher.defaultAction
openFile
--launcher.XXMaxPermSize
256M
-showsplash
org.eclipse.platform
--launcher.XXMaxPermSize
256m
--launcher.defaultAction
openFile
-vmargs
-Dosgi.requiredJavaVersion=1.5
-Xms256m
-Xmx2048m
-XX:PermSize=64m
-Xss1M
-server
-XX:+DoEscapeAnalysis
-XX:+UseConcMarkSweepGC
-XX:+UseCompressedOops

I am 99% sure this same code was taking 0.7 s to run in Eclipse a couple of days ago. I have tried pasting the code into a new project and running from there, but it doesn't change the running time. The project uses the global Scala Compiler settings and those are all set to default.

UPDATE : I have just tried swapping in the 2.9.0.1 Scala Library so that it's using the exact same version as the command line, and it made no difference to run time.

I also tried running the file from the command line with all the -vmargs options above, and it makes no difference.

Any ideas what is going on, or any settings I need to check? Thanks.


UPDATE 2 : I've half-answered the question myself now - I was running the code in different ways; but here's the problem:

Version 1, extending App :

object P005_V2 extends App { 
  def isDivis(x:Int, i:Int):Boolean = {
    if(i > 20) true
    else if(x % i != 0) false
    else isDivis(x, i+1)
  }
  def find(n:Int):Int = if (isDivis(n, 2)) n else find (n+2)

  val t = System.nanoTime;
  println (find (2))
  println(System.nanoTime - t)
}

Takes 0.7 s in Eclipse, 2.3 s from command prompt

Version 2, instantiating from another object

object P005_V2 { 
  def isDivis(x:Int, i:Int):Boolean = {
    if(i > 20) true
    else if(x % i != 0) false
    else isDivis(x, i+1)
  }
  def find(n:Int):Int = if (isDivis(n, 2)) n else find (n+2)

  val t = System.nanoTime;
  println (find (2))
  println(System.nanoTime - t)
}

object Run extends App {
  P005_V2
}

Takes 7.6 s in Eclipse, 2.5 s from command line

So, I understand that App was designed so that you can do things in the constructor and they will be optimized, unlike the deprecated Application. What seems to be happening here is that because I'm calling println(find(2)) from the constructor, the version which doesn't extend Appwill not be optimized and will take longer.

But the question remains, why the huge speed differences between executing via Eclipse and the command line?

Indeed both the command line versions are running slow, because a Java version at the command line takes 0.7 s and the Scala version should be as fast (as it can be, as demonstrated by version 1 running in Eclipse).

解决方案

After some experimentation I have 95% of the answer so I will give it myself:

I am running Windows 7 64-bit.

The command line is using the 32-bit JDK environment, as specified by JAVA_HOME variable.

Eclipse is using the 64-bit JRE environment, specified via Window | Preferences | Java | Installed JREs and the project's System Library.

Changing each of these to use the other version produces similar running times.

java from the command line will use the 64-bit JVM when available, regardless of what is specified in JAVA_HOME, unlike scala, which can lead to confusion in Scala/Java comparisons.

The difference in running times between Versions 1 and 2 above is because, as noted, Version 2 is running the costly calculation in the object's constructor (rather than a delayedInt method when App is used in Version 1). Version 2 is very wrong, since you shouldn't do costly calculations and I/O in a constructor. If you call the find method directly from the Run object then the speed anomaly goes away.

Conclusion:

Run times will depend on whether you are running the 32-bit or 64-bit JVM, and this is specified within Eclipse regardless of your environment settings. If using the 64-bit version you need to be especially careful not to do heavy processing within a constructor.

Running times (seconds):

                   Java                          Scala                         Scala - within constructor
                   JRE 64-bit     JDK 32-bit     JRE 64-bit     JDK 32-bit     JRE 64-bit     JDK 32-bit
Windows 7 64-bit   0.7            2.4            0.7            2.5            7.6            2.5
Windows XP 32-bit  n/a            13.4           n/a            14             n/a            13.1
(slower machine)

这篇关于Eclipse中的Scala执行时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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