依稀还记得,try{}之后的代码块应尽量越少越好,难道代码被try{}catch(){}包围之后会降低运行效率吗?
测试一下:
package exception; public class TestTry_Catch { public static boolean try_catch() { try { return true; } catch (Exception e) { return false; } } public static boolean no_try_catch() { return true; } public static void main(String[] args) { long current1 = System.currentTimeMillis(); long time = 10000000000l; for (long i = 0l; i < time; i++) try_catch(); long current2 = System.currentTimeMillis(); for (long j = 0l; j < time; j++) no_try_catch(); long current3 = System.currentTimeMillis(); System.out.println("try_catch time : " + (current2 - current1) + " ms"); System.out.println("no_try_catch time : " + (current3 - current2) + " ms"); } }
下面是console输出:
try_catch time : 4843 ms no_try_catch time : 4683 ms
这么多次,相差依然很小,在不会发生异常的情况下try_catch方法和no_try_catch方法运行的时间基本相差无几,在即时编译时try_catch方法比no_try_catch方法应花费稍多时间
下面再看一个更有趣的,当把return type改为void 时,代码:
package exception; public class TestTry_Catch { public static void try_catch() { try { // return true; } catch (Exception e) { // return false; } } public static void no_try_catch() { // return true; } public static void main(String[] args) { long current1 = System.currentTimeMillis(); long time = 10000000000l; for (long i = 0l; i < time; i++) try_catch(); long current2 = System.currentTimeMillis(); for (long j = 0l; j < time; j++) no_try_catch(); long current3 = System.currentTimeMillis(); System.out.println("try_catch time : " + (current2 - current1) + " ms"); System.out.println("no_try_catch time : " + (current3 - current2) + " ms"); } }
console 输出:
try_catch time : 4772 ms no_try_catch time : 4918 ms
try_catch 比no_try_catch花费的时间更少了,这该怎样解释呢?
让方法进行一些计算吧,不去创建对象,因为可能会触发GC,只是去计算一下1+1
package exception; public class TestTry_Catch { public static void try_catch() { try { int a = 1 + 1; int b = a + 3; } catch (Exception e) { // return false; } } public static void no_try_catch() { int a = 1 + 1; int b = a + 3; } public static void main(String[] args) { long current1 = System.currentTimeMillis(); long time = 10000000000l; for (long i = 0l; i < time; i++) try_catch(); long current2 = System.currentTimeMillis(); for (long j = 0l; j < time; j++) no_try_catch(); long current3 = System.currentTimeMillis(); System.out.println("try_catch time : " + (current2 - current1) + " ms"); System.out.println("no_try_catch time : " + (current3 - current2) + " ms"); } }
console输出:
try_catch time : 4957 ms no_try_catch time : 4934 ms
我开始怀疑 try{}catch(){}代码块越短越好 这句话的正确性了
下面测一下异常对象产的的耗时和普通对象产生的耗时比较:
package exception; import exception.entity.My_Exception; public class New_Object_Exception { public static void main(String[] args) { for (int i = 0; i < 10000; i++) new Throwable(); long number = 100000l; long current1 = System.currentTimeMillis(); for (long j = 0l; j < number; j++) new My_Exception(); long current2 = System.currentTimeMillis(); for (long i = 0l; i < number; i++) new Exception(); long current3 = System.currentTimeMillis(); System.out.println("new My_Exception time : " + (current2 - current1) + " ms"); System.out.println("new Exception time : " + (current3 - current2) + " ms"); } }
console输出:
new My_Exception time : 113 ms new Exception time : 144 ms
相差很小,一个数量级,无视GC了
下面测一下抛出异常到接收到异常的时间,代码如下:
package exception; import exception.entity.My_Exception; public class Catch_Exception { public static void main(String[] args) { long time = 100000l; for (int i = 0; i < 10000; i++) { new Throwable(); } long current1 = System.currentTimeMillis(); for (long i = 0l; i < time; i++) { try { throw new Exception(); } catch (Exception e) { } } long current2 = System.currentTimeMillis(); for (long i = 0l; i < time; i++) { try { throw new My_Exception(); } catch (My_Exception e) { } } long current3 = System.currentTimeMillis(); System.out.println("catch Exception : " + (current2 - current1) + " ms"); System.out.println("catch My_Exception : " + (current3 - current2) + " ms"); } }
console输出:
catch Exception : 77 ms catch My_Exception : 64 ms
贴一下jdk中Throwable 类的getMessage(), getStackTrace()源码
getMessage():
public String getMessage() { return detailMessage; }
detailMessage只是Throwable类的一个私有变量
private String detailMessage;
getStackTrace():
public StackTraceElement[] getStackTrace() { return getOurStackTrace().clone(); }
getOurStackTrace():
private synchronized StackTraceElement[] getOurStackTrace() { // Initialize stack trace field with information from // backtrace if this is the first call to this method if (stackTrace == UNASSIGNED_STACK || (stackTrace == null && backtrace != null) /* Out of protocol state */) { int depth = getStackTraceDepth(); stackTrace = new StackTraceElement[depth]; for (int i=0; i < depth; i++) stackTrace[i] = getStackTraceElement(i); } else if (stackTrace == null) { return UNASSIGNED_STACK; } return stackTrace; }
如果不是自定义的异常对象,大概没必要去调这个方法,如果不调这个方法抛出异常,捕获异常,貌似对性能也没什么影响
关于异常,各有各的想法吧
另外我的电脑配置:i5 win7 64位 4G内存, jdk : jdk1.7.0_10 b18 64位
相关推荐
} catch (Exception e) { e.printStackTrace(); return null; } finally { try { if (fileInputStream != null){ fileInputStream.close(); } } catch (IOException e) { e....
与 Exception 有关的 Java 关键字 throws 和 throw try 、finally 、catch 什么是 Error 内部类 创建内部类集合 Iterable 接口顶层接口 ArrayList Vector LinkedList 类Stack HashSet TreeSet LinkedHashSet 类 ...
它的主要意图就要将日志记录,性能统计,安全控制等等代码从商业逻辑代码中清楚的划分出来,我们可以把这些行为一个一个单独看作系统所要解决的问题,就是所谓的面向问题的编程(不知将AOP译作面向问题的编程是否欠妥...
一、Java基础知识 1.Java有那些基本数据类型,String是不是基本数据类型,他们有何区别。 2.字符串的操作: 写一个方法,实现字符串的反转,如:输入abc,输出cba 写一个方法,实现字符串的替换,如:输入...
GC是垃圾收集的意思(Gabage Collection),内存处理是编程人员容易出现问题的地方,忘记或者错误的内存回收会导致程序或系统的不稳定甚至崩溃,Java提供的GC功能可以自动监测对象是否超过作用域从而达到自动回收...
64、说出ArrayList,Vector, LinkedList的存储性能和特性 46 65、去掉一个Vector集合中重复的元素 46 66、Collection 和 Collections的区别。 47 67、Set里的元素是不能重复的,那么用什么方法来区分重复与否呢? 是用...
64、说出ArrayList,Vector, LinkedList的存储性能和特性 46 65、去掉一个Vector集合中重复的元素 46 66、Collection 和 Collections的区别。 47 67、Set里的元素是不能重复的,那么用什么方法来区分重复与否呢? 是用...
使用JAVA中效率高的类,比如Array List比Vector性能好.>高并发 - 需要解决的问题一:应用缓存二:缓存三:多级缓存四:池化五:异步并发六:扩容七 :队列高并发-应用缓存堆缓存 使用Java堆内存来存储缓存对象....
4. 说出ArrayList,Vector,LinkedList的存储性能和特性 8 5. EJB是基于哪些技术实现的?并说出SessionBean和EntityBean的区别,StatefulBean和StatelessBean的区别。 9 6. Collection 和 Collections的区别。 9 7. &...
64、说出ArrayList,Vector, LinkedList的存储性能和特性 46 65、去掉一个Vector集合中重复的元素 46 66、Collection 和 Collections的区别。 47 67、Set里的元素是不能重复的,那么用什么方法来区分重复与否呢? 是用...
4 性能(Performance) 97 實踐28:先把焦點放在設計、數據結構和算法身㆖99 實踐29:不要倚賴編譯期(compile-time)優化技術 101 實踐30:理解運行期(runtime)代碼優化技術105 實踐31:如欲進行字符串接合,...
67、说出ArrayList,Vector, LinkedList的存储性能和特性 17 68、java中有几种类型的流?JDK为每种类型的流提供了一些抽象类以供继承,请说出他们分别是哪些类? 17 69、文件读写的基本类 17 70、多线程有几种实现...
Java NIO非堵塞应用通常适用用在I/O读写等方面,我们知道,系统运行的性能瓶颈通常在I/O读写,包括对端口和文件的操作上,过去,在打开一个I/O通道后,read()将一直等待在端口一边读取字节内容,如果没有内容进来,...
2.2.1基本性能 6 2.2.2开发目标 7 2.2.3 应用目标 7 2.3选择编程语言 7 2.3.1 JSP与ASP的比较 7 2.3.2 JSP的特点与优点 8 3系统设计 10 3.1系统设计思想 10 3.1.1 网上论坛系统说明 10 3.1.2 网上论坛系统总体分析 ...
1、良好的统计性能。可以统计各种方式的注释,如单行、多行注释;注释在行前 、行尾、单独成行等。 2、可以进行多种语言注释统计。默认情况下已经可以统计.java、.C、.h、.CPP 、.C++和.AS 类型文件的统计了。...
1. 传输数据时间短,性能高 2. 可以加密,数据安全性强 3. 适合客户端和服务器端之间信息实时交互 4. 传输数据为字节级,传输数据可自定义,数据量小 ## 1.3 缺点 相对于http协议传输,增加了开发量 服务端 1. *...
2. 避免使用错误的方式,如Exception可以控制方法推出,但是Exception要保留stacktrace消耗性能,除非必要不要使用 instanceof做条件判断,尽量使用比的条件判断方式.使用JAVA中效率高的类,比如ArrayList比Vector性能好...
SeimiCrawler是一个敏捷的,支持分布式的Java爬虫开发框架,希望能在最大程度上降低新手开发一个可用性高且性能不差的爬虫系统的门槛,以及提升开发爬虫系统的开发效率。在SeimiCrawler的世界里,绝大多数人只需...
这几天,用java写了一个无限极的树,递归写的,可能代码不够简洁,性能不够好,不过也算是练习,这几天再不断改进。前面几个小图标的判断,搞死我了。 package com.nickol.servlet; import java.io....