前言
对于大对象 效率:clone > new > 反序列化
对于小对象 效率:new > clone > 反序列化
package com.dashu.application; import com.dashu.bean.Cat; import com.dashu.bean.HeavyBean; import com.dashu.utils.CloneUtil; import org.junit.jupiter.api.Test; /** * @Auther: DaShu * @Date: 2021/7/22 17:21 * @Description: */ public class SampleTwo { /** * 序列化是永远是最慢的,超大时 clone > new ,超小时,new > clone * */ @Test public void testOne() throws Exception { //超大型对象,效率:clone > 序列化 > new HeavyBean heavyBean1 = null; CloneUtil<HeavyBean> heavyBeanCloneUtil = new CloneUtil<HeavyBean>(); long oneTime = System.nanoTime(); for (int i = 0; i < 10; i++) { heavyBean1 = new HeavyBean(); } long twoTime = System.nanoTime(); System.out.println((twoTime - oneTime) / 10 + "nm");//49121890nm long threeTime = System.nanoTime(); for (int i = 0; i < 10; i++) { HeavyBean heavyBean2 = (HeavyBean) heavyBean1.clone(); } long fourTime = System.nanoTime(); System.out.println((fourTime - threeTime) / 10 + "nm");//1080nm long fiveTime = System.nanoTime(); for (int i = 0; i < 10; i++) { HeavyBean heavyBean3 = heavyBeanCloneUtil.Clone(heavyBean1); } long sixTime = System.nanoTime(); System.out.println((sixTime - fiveTime) / 10 + "nm");//1763650nm } @Test public void testTwo() throws Exception { //超小型对象,效率:clone > new > 序列化 Cat cat = null; CloneUtil<Cat> heavyBeanCloneUtil = new CloneUtil<Cat>(); long oneTime = System.nanoTime(); for (int i = 0; i < 10; i++) { cat = new Cat(); } long twoTime = System.nanoTime(); System.out.println((twoTime - oneTime) / 10 + "nm"); long threeTime = System.nanoTime(); for (int i = 0; i < 10; i++) { Cat cat2= (Cat) cat.clone(); } long fourTime = System.nanoTime(); System.out.println((fourTime - threeTime) / 10 + "nm"); long fiveTime = System.nanoTime(); for (int i = 0; i < 10; i++) { Cat cat3= heavyBeanCloneUtil.Clone(cat); } long sixTime = System.nanoTime(); System.out.println((sixTime - fiveTime) / 10 + "nm"); } @Test public void testThree() throws CloneNotSupportedException { //超小对象 new > clone class Tree implements Cloneable { @Override protected Object clone() throws CloneNotSupportedException { return super.clone(); } } long oneTime = System.nanoTime(); Tree tree = new Tree(); long twoTime = System.nanoTime(); System.out.println(twoTime - oneTime + "nm"); long threeTime = System.nanoTime(); Tree t = (Tree) tree.clone(); long fourTime = System.nanoTime(); System.out.println(fourTime - threeTime + "nm"); } public static void main(String[] args) throws CloneNotSupportedException{ int count = 10000 * 1000; class Bean implements Cloneable { private String name; public Bean(String name) { this.name = name; } @Override protected Bean clone() throws CloneNotSupportedException { return (Bean) super.clone(); } } long s1 = System.nanoTime(); for (int i = 0; i < count; i++) { Bean bean = new Bean("ylWang"); } long s2 = System.nanoTime(); Bean bean = new Bean("ylWang"); for (int i = 0; i < count; i++) { Bean b = bean.clone(); } long s3 = System.nanoTime(); System.out.println("new = " + (s2 - s1)/count + "nm"); System.out.println("clone = " + (s3 - s2)/count + "nm"); } }