java Clone和New哪个更快

标签:
杂谈 |
分类: java |
Clone和new哪个更快呢,这个问题的答案不是一定的,要根据实际情况决定:
情况一:对象简单,这个时候new更快,测试代码如下:
情况一:对象简单,这个时候new更快,测试代码如下:
http://www.blogjava.net/Images/dot.gifClone和New哪个更快" TITLE="java Clone和New哪个更快" />{
http://www.blogjava.net/Images/dot.gifClone和New哪个更快" TITLE="javaClone和New哪个更快" /> {
http://www.blogjava.net/Images/OutliningIndicators/InBlock.gifClone和New哪个更快" TITLE="javaClone和New哪个更快" /> Object obj = null ;
http://www.blogjava.net/Images/dot.gifClone和New哪个更快" TITLE="javaClone和New哪个更快" /> {
http://www.blogjava.net/Images/OutliningIndicators/InBlock.gifClone和New哪个更快" TITLE="javaClone和New哪个更快" /> obj = super.clone();
http://www.blogjava.net/Images/dot.gifClone和New哪个更快" TITLE="javaClone和New哪个更快" /> {
http://www.blogjava.net/Images/OutliningIndicators/ExpandedSubBlockEnd.gifClone和New哪个更快" TITLE="javaClone和New哪个更快" /> }
http://www.blogjava.net/Images/OutliningIndicators/InBlock.gifClone和New哪个更快" TITLE="javaClone和New哪个更快" /> return obj;
http://www.blogjava.net/Images/OutliningIndicators/ExpandedSubBlockEnd.gifClone和New哪个更快" TITLE="javaClone和New哪个更快" /> }
http://www.blogjava.net/Images/OutliningIndicators/ExpandedBlockEnd.gifClone和New哪个更快" TITLE="javaClone和New哪个更快" />}
http://www.blogjava.net/Images/dot.gifClone和New哪个更快" TITLE="javaClone和New哪个更快" /> {
http://www.blogjava.net/Images/dot.gifClone和New哪个更快" TITLE="javaClone和New哪个更快" /> {
http://www.blogjava.net/Images/OutliningIndicators/InBlock.gifClone和New哪个更快" TITLE="javaClone和New哪个更快" /> TestObj obj =new TestObj();
http://www.blogjava.net/Images/dot.gifClone和New哪个更快" TITLE="javaClone和New哪个更快" /> {
http://www.blogjava.net/Images/OutliningIndicators/InBlock.gifClone和New哪个更快" TITLE="javaClone和New哪个更快" /> obj.clone();
http://www.blogjava.net/Images/OutliningIndicators/ExpandedSubBlockEnd.gifClone和New哪个更快" TITLE="javaClone和New哪个更快" /> }
http://www.blogjava.net/Images/OutliningIndicators/ExpandedSubBlockEnd.gifClone和New哪个更快" TITLE="javaClone和New哪个更快" /> }
http://www.blogjava.net/Images/OutliningIndicators/InBlock.gifClone和New哪个更快" TITLE="javaClone和New哪个更快" />
http://www.blogjava.net/Images/dot.gifClone和New哪个更快" TITLE="javaClone和New哪个更快" /> {
http://www.blogjava.net/Images/OutliningIndicators/InBlock.gifClone和New哪个更快" TITLE="javaClone和New哪个更快" /> TestObj obj =new TestObj();
http://www.blogjava.net/Images/dot.gifClone和New哪个更快" TITLE="javaClone和New哪个更快" /> {
http://www.blogjava.net/Images/OutliningIndicators/InBlock.gifClone和New哪个更快" TITLE="javaClone和New哪个更快" /> obj =new TestObj();
http://www.blogjava.net/Images/OutliningIndicators/ExpandedSubBlockEnd.gifClone和New哪个更快" TITLE="javaClone和New哪个更快" /> }
http://www.blogjava.net/Images/OutliningIndicators/ExpandedSubBlockEnd.gifClone和New哪个更快" TITLE="javaClone和New哪个更快" /> }
http://www.blogjava.net/Images/OutliningIndicators/ContractedSubBlock.gifClone和New哪个更快" TITLE="javaClone和New哪个更快" />
http://www.blogjava.net/Images/dot.gifClone和New哪个更快" TITLE="javaClone和New哪个更快" /> {
http://www.blogjava.net/Images/OutliningIndicators/InBlock.gifClone和New哪个更快" TITLE="javaClone和New哪个更快" /> long start;
http://www.blogjava.net/Images/OutliningIndicators/InBlock.gifClone和New哪个更快" TITLE="javaClone和New哪个更快" /> long stop;
http://www.blogjava.net/Images/OutliningIndicators/InBlock.gifClone和New哪个更快" TITLE="javaClone和New哪个更快" /> int times =1000000;
http://www.blogjava.net/Images/OutliningIndicators/InBlock.gifClone和New哪个更快" TITLE="javaClone和New哪个更快" />
http://www.blogjava.net/Images/OutliningIndicators/InBlock.gifClone和New哪个更快" TITLE="javaClone和New哪个更快" /> System.gc();
http://www.blogjava.net/Images/OutliningIndicators/InBlock.gifClone和New哪个更快" TITLE="javaClone和New哪个更快" />
http://www.blogjava.net/Images/OutliningIndicators/InBlock.gifClone和New哪个更快" TITLE="javaClone和New哪个更快" />
http://www.blogjava.net/Images/OutliningIndicators/InBlock.gifClone和New哪个更快" TITLE="javaClone和New哪个更快" /> start =System.currentTimeMillis();
http://www.blogjava.net/Images/OutliningIndicators/InBlock.gifClone和New哪个更快" TITLE="javaClone和New哪个更快" /> newTest(times);
http://www.blogjava.net/Images/OutliningIndicators/InBlock.gifClone和New哪个更快" TITLE="javaClone和New哪个更快" /> stop =System.currentTimeMillis();
http://www.blogjava.net/Images/OutliningIndicators/InBlock.gifClone和New哪个更快" TITLE="javaClone和New哪个更快" /> System.out.println( "newTest Time: "+(stop-start));
http://www.blogjava.net/Images/OutliningIndicators/InBlock.gifClone和New哪个更快" TITLE="javaClone和New哪个更快" />
http://www.blogjava.net/Images/OutliningIndicators/InBlock.gifClone和New哪个更快" TITLE="javaClone和New哪个更快" /> System.gc();
http://www.blogjava.net/Images/OutliningIndicators/InBlock.gifClone和New哪个更快" TITLE="javaClone和New哪个更快" />
http://www.blogjava.net/Images/OutliningIndicators/InBlock.gifClone和New哪个更快" TITLE="javaClone和New哪个更快" /> start =System.currentTimeMillis();
http://www.blogjava.net/Images/OutliningIndicators/InBlock.gifClone和New哪个更快" TITLE="javaClone和New哪个更快" /> cloneTest(times);
http://www.blogjava.net/Images/OutliningIndicators/InBlock.gifClone和New哪个更快" TITLE="javaClone和New哪个更快" /> stop =System.currentTimeMillis();
http://www.blogjava.net/Images/OutliningIndicators/InBlock.gifClone和New哪个更快" TITLE="javaClone和New哪个更快" /> System.out.println( "cloneTest Time: "+(stop-start));
http://www.blogjava.net/Images/OutliningIndicators/InBlock.gifClone和New哪个更快" TITLE="javaClone和New哪个更快" />
http://www.blogjava.net/Images/OutliningIndicators/ExpandedSubBlockEnd.gifClone和New哪个更快" TITLE="javaClone和New哪个更快" /> }
http://www.blogjava.net/Images/OutliningIndicators/InBlock.gifClone和New哪个更快" TITLE="javaClone和New哪个更快" />
http://www.blogjava.net/Images/OutliningIndicators/ExpandedBlockEnd.gifClone和New哪个更快" TITLE="javaClone和New哪个更快" />}
http://www.blogjava.net/Images/dot.gifClone和New哪个更快" TITLE="java
http://www.blogjava.net/Images/OutliningIndicators/InBlock.gifClone和New哪个更快" TITLE="java
http://www.blogjava.net/Images/dot.gifClone和New哪个更快" TITLE="java
http://www.blogjava.net/Images/OutliningIndicators/InBlock.gifClone和New哪个更快" TITLE="java
http://www.blogjava.net/Images/dot.gifClone和New哪个更快" TITLE="java
http://www.blogjava.net/Images/OutliningIndicators/ExpandedSubBlockEnd.gifClone和New哪个更快" TITLE="java
http://www.blogjava.net/Images/OutliningIndicators/InBlock.gifClone和New哪个更快" TITLE="java
http://www.blogjava.net/Images/OutliningIndicators/ExpandedSubBlockEnd.gifClone和New哪个更快" TITLE="java
http://www.blogjava.net/Images/OutliningIndicators/ExpandedBlockEnd.gifClone和New哪个更快" TITLE="java
http://www.blogjava.net/Images/dot.gifClone和New哪个更快" TITLE="java
http://www.blogjava.net/Images/dot.gifClone和New哪个更快" TITLE="java
http://www.blogjava.net/Images/OutliningIndicators/InBlock.gifClone和New哪个更快" TITLE="java
http://www.blogjava.net/Images/dot.gifClone和New哪个更快" TITLE="java
http://www.blogjava.net/Images/OutliningIndicators/InBlock.gifClone和New哪个更快" TITLE="java
http://www.blogjava.net/Images/OutliningIndicators/ExpandedSubBlockEnd.gifClone和New哪个更快" TITLE="java
http://www.blogjava.net/Images/OutliningIndicators/ExpandedSubBlockEnd.gifClone和New哪个更快" TITLE="java
http://www.blogjava.net/Images/OutliningIndicators/InBlock.gifClone和New哪个更快" TITLE="java
http://www.blogjava.net/Images/dot.gifClone和New哪个更快" TITLE="java
http://www.blogjava.net/Images/OutliningIndicators/InBlock.gifClone和New哪个更快" TITLE="java
http://www.blogjava.net/Images/dot.gifClone和New哪个更快" TITLE="java
http://www.blogjava.net/Images/OutliningIndicators/InBlock.gifClone和New哪个更快" TITLE="java
http://www.blogjava.net/Images/OutliningIndicators/ExpandedSubBlockEnd.gifClone和New哪个更快" TITLE="java
http://www.blogjava.net/Images/OutliningIndicators/ExpandedSubBlockEnd.gifClone和New哪个更快" TITLE="java
http://www.blogjava.net/Images/OutliningIndicators/ContractedSubBlock.gifClone和New哪个更快" TITLE="java
http://www.blogjava.net/Images/dot.gifClone和New哪个更快" TITLE="java
http://www.blogjava.net/Images/OutliningIndicators/InBlock.gifClone和New哪个更快" TITLE="java
http://www.blogjava.net/Images/OutliningIndicators/InBlock.gifClone和New哪个更快" TITLE="java
http://www.blogjava.net/Images/OutliningIndicators/InBlock.gifClone和New哪个更快" TITLE="java
http://www.blogjava.net/Images/OutliningIndicators/InBlock.gifClone和New哪个更快" TITLE="java
http://www.blogjava.net/Images/OutliningIndicators/InBlock.gifClone和New哪个更快" TITLE="java
http://www.blogjava.net/Images/OutliningIndicators/InBlock.gifClone和New哪个更快" TITLE="java
http://www.blogjava.net/Images/OutliningIndicators/InBlock.gifClone和New哪个更快" TITLE="java
http://www.blogjava.net/Images/OutliningIndicators/InBlock.gifClone和New哪个更快" TITLE="java
http://www.blogjava.net/Images/OutliningIndicators/InBlock.gifClone和New哪个更快" TITLE="java
http://www.blogjava.net/Images/OutliningIndicators/InBlock.gifClone和New哪个更快" TITLE="java
http://www.blogjava.net/Images/OutliningIndicators/InBlock.gifClone和New哪个更快" TITLE="java
http://www.blogjava.net/Images/OutliningIndicators/InBlock.gifClone和New哪个更快" TITLE="java
http://www.blogjava.net/Images/OutliningIndicators/InBlock.gifClone和New哪个更快" TITLE="java
http://www.blogjava.net/Images/OutliningIndicators/InBlock.gifClone和New哪个更快" TITLE="java
http://www.blogjava.net/Images/OutliningIndicators/InBlock.gifClone和New哪个更快" TITLE="java
http://www.blogjava.net/Images/OutliningIndicators/InBlock.gifClone和New哪个更快" TITLE="java
http://www.blogjava.net/Images/OutliningIndicators/InBlock.gifClone和New哪个更快" TITLE="java
http://www.blogjava.net/Images/OutliningIndicators/InBlock.gifClone和New哪个更快" TITLE="java
http://www.blogjava.net/Images/OutliningIndicators/InBlock.gifClone和New哪个更快" TITLE="java
http://www.blogjava.net/Images/OutliningIndicators/ExpandedSubBlockEnd.gifClone和New哪个更快" TITLE="java
http://www.blogjava.net/Images/OutliningIndicators/InBlock.gifClone和New哪个更快" TITLE="java
http://www.blogjava.net/Images/OutliningIndicators/ExpandedBlockEnd.gifClone和New哪个更快" TITLE="java
情况二:对象复杂,例如一个包括集合类的类的对象。而且这个对象的Clone使用的浅拷贝。(其实快主要是快在这个地方)
不用例子了,浅拷贝只是引用的复制,肯定比复制快。
还有一些其它的情况,但总体来说,随着对象的复杂,clone越来越快,new越来越慢。不过在使用clone的时候
一定要想清楚再用,浅拷贝使用不当会出现很多问题。