当我从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,扩展应用程序:
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秒。
第二版本,从另一个对象实例化
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
}
在Eclipse中需要7.6秒,在命令行中需要2.5秒。
因此,我了解到该应用程序的设计是可以在构造函数中执行优化操作,而不像已弃用的
Application
。这里似乎发生的情况是,因为我从构造函数中调用println(find(2))
,没有扩展App
的版本不会被优化,所以需要更长时间。但问题仍然存在,为什么通过Eclipse和命令行执行会有如此巨大的速度差异?
实际上,两个命令行版本都运行缓慢,因为在命令行中运行的Java版本需要0.7秒,而Scala版本应该与之一样快(正如在Eclipse中演示的版本1所示)。
eclipse.ini
зҡ„EclipseиҝҗиЎҢеҗ—пјҲе…·дҪ“жқҘиҜҙпјҢдҪҝз”ЁJDKзҡ„dllиҖҢдёҚжҳҜJREзҡ„jarпјүпјҡhttps://dev59.com/questions/1XVC5IYBdhLWcg3w7V33#3275659 - VonC