try catch 效率
long start = System.currentTimeMillis(); for (int i = 0; i < 1000000; i++) { } long end = System.currentTimeMillis(); System.out.println("---:"+(end-start)); long start1 = System.currentTimeMillis(); for (int i = 0; i < 1000000; i++) { try { } catch (Exception e) { } } long end1 = System.currentTimeMillis(); System.out.println("---:"+(end1-start1)); long start2 = System.currentTimeMillis(); for (int i = 0; i < 1000000; i++) { try { throw new Exception(); } catch (Exception e) { } } long end2 = System.currentTimeMillis(); System.out.println("---:"+(end2-start2));
---:1
---:1
---:795
try catch只要不抛异常就对效率影响不大。
-------------------------------------------------------------
在第一个循环里加入system.out.print("");其余两个循环try catch里也加入system.out.print("");
---:136
---:130
---:920
堆栈跟踪
昨天在服务器给一段代码加上了。try catch ..结果。。try catch 后程序运行的时间原来是20-100ms 结果现在是400多ms.。。。不晓得原因。。效率问题。。