大量for循环中new对象与clone效率对比

上传人:大米 文档编号:463251004 上传时间:2023-11-29 格式:DOC 页数:2 大小:71KB
返回 下载 相关 举报
大量for循环中new对象与clone效率对比_第1页
第1页 / 共2页
大量for循环中new对象与clone效率对比_第2页
第2页 / 共2页
亲,该文档总共2页,全部预览完了,如果喜欢就下载吧!
资源描述

《大量for循环中new对象与clone效率对比》由会员分享,可在线阅读,更多相关《大量for循环中new对象与clone效率对比(2页珍藏版)》请在金锄头文库上搜索。

1、大量for循环中new对象与clone效率对比“创建实体类,实现Cloneable接口,重写被保护的Object的clone()方法实现对比publicstaticvoidmain(Stringargs)ListuserList1=newArrayList();ListuserList2=newArrayList();longt1=System.currentTimeMillis();for(inti=0;i1000000;i+)Useru1=newUser();u1.setAge(10);u1.setName(Tom+i);userList1.add(u1);longt2=System.cu

2、rrentTimeMillis();Useru1=newUser();tryfor(inti=0;i1000000;i+)Useru2=u1.clone();每次clone后是创建新的对象u2.setAge(20);u2.setName(Tom+i);userList2.add(u2);catch(CloneNotSupportedExceptione)e.printStackTrace();longt3=System.currentTimeMillis();System.out.println(t2-11);System.out.println(t3-12);“在数据了大的情况下,如果在循环内直接new对象,效率会非常低下,可以采用原型模式,先new出一个对象来,后面全部通过clone操作进行,也就是浅拷贝,效率会大幅提升。

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 办公文档 > 解决方案

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号