考虑以下代码:
public class JMain {
private final static int forLength = 6;
private static void returnFromFor(int breakAt) {
try {
for (int j = 0; j < forLength; j++) {
if (j == breakAt) {
throw new Throwable("break");
}
}
} catch (Throwable ignored) {
}
}
public static void main(String args[]) {
long start = System.currentTimeMillis();
int iterations = 100000;
for (int i = 0; i < iterations; i++) {
returnFromFor(i % forLength);
}
long end = System.currentTimeMillis();
System.out.println(((Long) (end - start)).toString() + " ms");
}
}
当我在启用了异常断点的Intellij IDEA调试器中运行此代码时,
java.lang.IllegalArgumentException
,运行非常慢,在我的电脑上大约1800毫秒。当我运行它时没有异常断点,或者启用了断点
java.lang.UnsupportedOperationException
,大约快10倍。
我的问题是:
运行此操作的系统是:
-
Windows X64
-
Intellij Idea Ultimate 2018.2版
-
JRE:1.8.0_-release-1248-B8 AMD64
注意:上面的代码演示了我在复杂scala中遇到的问题。如果有人对更自然的scala代码演示感兴趣,请看:
object Main extends App {
val forLength = 6
def returnFromFor(breakAt: Int): Unit = {
for (j <- 0 until forLength) {
if (j == breakAt) return
}
}
val start = System.currentTimeMillis()
val iterations = 100000
for (i <- 0 until iterations) {
returnFromFor(i%forLength)
}
val end = System.currentTimeMillis()
println(s"Duration ${end-start} ms")
}