1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 |
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
堆栈跟踪
原文:http://www.cnblogs.com/keia/p/3566443.html