类的初始化scala.Predef
是懒惰的重量级操作,可能会导致应用程序意外减速,并且在时间很重要的情况下(如编程竞赛)会成为麻烦。
val a = new Array[Integer](10)
a(5) = 3 //slowdown on this line
scala.Predef
那么我可以在不更改代码的情况下仅使用 scala 编译器或 VM 选项关闭它的惰性并在应用程序启动时强制初始化吗?
类的初始化scala.Predef
是懒惰的重量级操作,可能会导致应用程序意外减速,并且在时间很重要的情况下(如编程竞赛)会成为麻烦。
val a = new Array[Integer](10)
a(5) = 3 //slowdown on this line
scala.Predef
那么我可以在不更改代码的情况下仅使用 scala 编译器或 VM 选项关闭它的惰性并在应用程序启动时强制初始化吗?
No you can't. You can initialize an object by calling it, like this
Predef // ensures the body of Predef is initialized
val a = new Array[Integer](10)
a(5) = 3
Still, you will probably not have initialized the ArrayOps
class which is involved in a.apply
. Lazy class initialization is a property of the JVM. If you do benchmarks, that's why you usually give it a "warmup" run first, so that all involved classes have been loaded first.