资源预览内容
第1页 / 共2页
第2页 / 共2页
亲,该文档总共2页全部预览完了,如果喜欢就下载吧!
资源描述
大量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.currentTimeMillis();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操作进行,也就是浅拷贝,效率会大幅提升。
收藏 下载该资源
网站客服QQ:2055934822
金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号