闲来无事,做个测试
先上测试代码
1抛异常的
static void f2() {
throw new RuntimeException("1");
}
public static void main(String[] args) {
long a = System.nanoTime();
for (int i = 0; i < 10000000; i++) {
try {
f2();
} catch (Exception e) {
String s=e.getMessage();
}
}
long b = System.nanoTime();
System.out.println(b-a);
}
2直接返回的
static String f1() {
return "1";
}
public static void main(String[] args) {
long a = System.nanoTime();
for (int i = 0; i < 10000000; i++) {
String s=f1();
}
long b = System.nanoTime();
System.out.println(b-a);
}
分别执行10次结果
[table]
|直接返回|抛异常
|10971157|10890656983
|9833024|10809811373
|12002224|10813899951
|15228402|10740746030
|65847475|10736142652
|9674623|10735044607
|8212356|11051365537
|10112668|10746780876
|9975290|10764845322
|11647779|10846855466
[/table]
分别去掉最大最小耗时求和得出
[table]
|直接返回|抛异常|抛异常/直接返回
|89445167|86349738653|965.3930061
[/table]