加载中…
个人资料
  • 博客等级:
  • 博客积分:
  • 博客访问:
  • 关注人气:
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
正文 字体大小:

java Clone和New哪个更快

(2010-02-03 18:00:50)
标签:

杂谈

分类: java
Clone和new哪个更快呢,这个问题的答案不是一定的,要根据实际情况决定:
情况一:对象简单,这个时候new更快,测试代码如下:
http://www.blogjava.net/Images/dot.gifClone和New哪个更快" TITLE="java Clone和New哪个更快" />{
http://www.blogjava.net/Images/dot.gifClone和New哪个更快" TITLE="java Clone和New哪个更快" />
{
http://www.blogjava.net/Images/OutliningIndicators/InBlock.gifClone和New哪个更快" TITLE="java Clone和New哪个更快" />        Object obj 
= null ;
http://www.blogjava.net/Images/dot.gifClone和New哪个更快" TITLE="java Clone和New哪个更快" />
{
http://www.blogjava.net/Images/OutliningIndicators/InBlock.gifClone和New哪个更快" TITLE="java Clone和New哪个更快" />            obj 
= super.clone();
http://www.blogjava.net/Images/dot.gifClone和New哪个更快" TITLE="java Clone和New哪个更快" />
{
http://www.blogjava.net/Images/OutliningIndicators/ExpandedSubBlockEnd.gifClone和New哪个更快" TITLE="java Clone和New哪个更快" />        }

http://www.blogjava.net/Images/OutliningIndicators/InBlock.gifClone和New哪个更快" TITLE="java Clone和New哪个更快" />        
return obj;  
http://www.blogjava.net/Images/OutliningIndicators/ExpandedSubBlockEnd.gifClone和New哪个更快" TITLE="java Clone和New哪个更快" />    }

http://www.blogjava.net/Images/OutliningIndicators/ExpandedBlockEnd.gifClone和New哪个更快" TITLE="java Clone和New哪个更快" />}

http://www.blogjava.net/Images/dot.gifClone和New哪个更快" TITLE="java Clone和New哪个更快" />
{
http://www.blogjava.net/Images/dot.gifClone和New哪个更快" TITLE="java Clone和New哪个更快" />
{
http://www.blogjava.net/Images/OutliningIndicators/InBlock.gifClone和New哪个更快" TITLE="java Clone和New哪个更快" />        TestObj obj
=new TestObj();
http://www.blogjava.net/Images/dot.gifClone和New哪个更快" TITLE="java Clone和New哪个更快" />
{
http://www.blogjava.net/Images/OutliningIndicators/InBlock.gifClone和New哪个更快" TITLE="java Clone和New哪个更快" />            obj.clone();
http://www.blogjava.net/Images/OutliningIndicators/ExpandedSubBlockEnd.gifClone和New哪个更快" TITLE="java Clone和New哪个更快" />        }

http://www.blogjava.net/Images/OutliningIndicators/ExpandedSubBlockEnd.gifClone和New哪个更快" TITLE="java Clone和New哪个更快" />    }

http://www.blogjava.net/Images/OutliningIndicators/InBlock.gifClone和New哪个更快" TITLE="java Clone和New哪个更快" />
http://www.blogjava.net/Images/dot.gifClone和New哪个更快" TITLE="java Clone和New哪个更快" />
{
http://www.blogjava.net/Images/OutliningIndicators/InBlock.gifClone和New哪个更快" TITLE="java Clone和New哪个更快" />        TestObj obj
=new TestObj();
http://www.blogjava.net/Images/dot.gifClone和New哪个更快" TITLE="java Clone和New哪个更快" />
{
http://www.blogjava.net/Images/OutliningIndicators/InBlock.gifClone和New哪个更快" TITLE="java Clone和New哪个更快" />            obj
=new TestObj();
http://www.blogjava.net/Images/OutliningIndicators/ExpandedSubBlockEnd.gifClone和New哪个更快" TITLE="java Clone和New哪个更快" />        }

http://www.blogjava.net/Images/OutliningIndicators/ExpandedSubBlockEnd.gifClone和New哪个更快" TITLE="java Clone和New哪个更快" />    }

http://www.blogjava.net/Images/OutliningIndicators/ContractedSubBlock.gifClone和New哪个更快" TITLE="java Clone和New哪个更快" />    

http://www.blogjava.net/Images/dot.gifClone和New哪个更快" TITLE="java Clone和New哪个更快" />
{
http://www.blogjava.net/Images/OutliningIndicators/InBlock.gifClone和New哪个更快" TITLE="java Clone和New哪个更快" />        
long start;
http://www.blogjava.net/Images/OutliningIndicators/InBlock.gifClone和New哪个更快" TITLE="java Clone和New哪个更快" />        
long stop;
http://www.blogjava.net/Images/OutliningIndicators/InBlock.gifClone和New哪个更快" TITLE="java Clone和New哪个更快" />        
int times=1000000;
http://www.blogjava.net/Images/OutliningIndicators/InBlock.gifClone和New哪个更快" TITLE="java Clone和New哪个更快" />        
http://www.blogjava.net/Images/OutliningIndicators/InBlock.gifClone和New哪个更快" TITLE="java Clone和New哪个更快" />        System.gc();
http://www.blogjava.net/Images/OutliningIndicators/InBlock.gifClone和New哪个更快" TITLE="java Clone和New哪个更快" />        
http://www.blogjava.net/Images/OutliningIndicators/InBlock.gifClone和New哪个更快" TITLE="java Clone和New哪个更快" />
http://www.blogjava.net/Images/OutliningIndicators/InBlock.gifClone和New哪个更快" TITLE="java Clone和New哪个更快" />        start
=System.currentTimeMillis();
http://www.blogjava.net/Images/OutliningIndicators/InBlock.gifClone和New哪个更快" TITLE="java Clone和New哪个更快" />        newTest(times);
http://www.blogjava.net/Images/OutliningIndicators/InBlock.gifClone和New哪个更快" TITLE="java Clone和New哪个更快" />        stop
=System.currentTimeMillis();
http://www.blogjava.net/Images/OutliningIndicators/InBlock.gifClone和New哪个更快" TITLE="java Clone和New哪个更快" />        System.out.println(
"newTest Time:"+(stop-start));
http://www.blogjava.net/Images/OutliningIndicators/InBlock.gifClone和New哪个更快" TITLE="java Clone和New哪个更快" />        
http://www.blogjava.net/Images/OutliningIndicators/InBlock.gifClone和New哪个更快" TITLE="java Clone和New哪个更快" />        System.gc();
http://www.blogjava.net/Images/OutliningIndicators/InBlock.gifClone和New哪个更快" TITLE="java Clone和New哪个更快" />        
http://www.blogjava.net/Images/OutliningIndicators/InBlock.gifClone和New哪个更快" TITLE="java Clone和New哪个更快" />        start
=System.currentTimeMillis();
http://www.blogjava.net/Images/OutliningIndicators/InBlock.gifClone和New哪个更快" TITLE="java Clone和New哪个更快" />        cloneTest(times);
http://www.blogjava.net/Images/OutliningIndicators/InBlock.gifClone和New哪个更快" TITLE="java Clone和New哪个更快" />        stop
=System.currentTimeMillis();
http://www.blogjava.net/Images/OutliningIndicators/InBlock.gifClone和New哪个更快" TITLE="java Clone和New哪个更快" />        System.out.println(
"cloneTest Time:"+(stop-start));
http://www.blogjava.net/Images/OutliningIndicators/InBlock.gifClone和New哪个更快" TITLE="java Clone和New哪个更快" />        
http://www.blogjava.net/Images/OutliningIndicators/ExpandedSubBlockEnd.gifClone和New哪个更快" TITLE="java Clone和New哪个更快" />    }

http://www.blogjava.net/Images/OutliningIndicators/InBlock.gifClone和New哪个更快" TITLE="java Clone和New哪个更快" />
http://www.blogjava.net/Images/OutliningIndicators/ExpandedBlockEnd.gifClone和New哪个更快" TITLE="java Clone和New哪个更快" />}


情况二:对象复杂,例如一个包括集合类的类的对象。而且这个对象的Clone使用的浅拷贝。(其实快主要是快在这个地方)
不用例子了,浅拷贝只是引用的复制,肯定比复制快。

还有一些其它的情况,但总体来说,随着对象的复杂,clone越来越快,new越来越慢。不过在使用clone的时候
一定要想清楚再用,浅拷贝使用不当会出现很多问题。

0

阅读 收藏 喜欢 打印举报/Report
  

新浪BLOG意见反馈留言板 欢迎批评指正

新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 产品答疑

新浪公司 版权所有