对象创建性能问题(new,clone,Serializable)

简介: 对象创建性能问题(new,clone,Serializable)


前言

对于大对象 效率: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");
    }
}


相关文章
|
设计模式 Java
Java克隆方式避免频繁创建对象优化方案
Java克隆方式避免频繁创建对象优化方案
137 0
|
9月前
|
Java
java反射-使用Class对象创建对象
java反射-使用Class对象创建对象
116 0
|
Java 关系型数据库 MySQL
抽象类与接口的比较?构造方法,构造方法重载,什么是复制构造方法?求N的阶乘?Java环境搭建:JDK、JRE、JVM关系?MySQL事务并发三大问题,针对事务并发的问题、java接口详情
抽象类与接口的比较?构造方法,构造方法重载,什么是复制构造方法?求N的阶乘?Java环境搭建:JDK、JRE、JVM关系? 抽象类与接口的比较
139 1
|
JSON Java 数据格式
Java - 关于 Cloneable 接口 clone 方法(二)
Java - 关于 Cloneable 接口 clone 方法(二)
120 0
Java - 关于 Cloneable 接口 clone 方法(二)
|
Java
Java - 关于 Cloneable 接口 clone 方法(一)
Java - 关于 Cloneable 接口 clone 方法(一)
184 0
|
存储 安全 Java
ConcurrentHashMap源码解析_01 成员属性、内部类、构造方法分析
ConcurrentHashMap源码解析_01 成员属性、内部类、构造方法分析
ConcurrentHashMap源码解析_01 成员属性、内部类、构造方法分析
|
JSON Java 数据格式
运用Java 反射机制实现对象克隆及类属性的克隆
根据业务需求调用核心业务系统接口,其中接口要求传输JSON数据格式,将Java对象数据直接转换为JSON数据格式;同时该Java对象随时都有可能根据业务需求变动被增加新的数据字段,而调用接口时并不需要额外的字段,如果再用该Java对象数据转换的JSON数据,就会出现调用接口参数不正确的情况,
684 0
|
安全 Java
高并发之——如何安全的发布对象(含各种单例代码分析)
首先,来介绍两个概念: 发布对象:使一个对象能够被当前范围之外的代码所使用。 对象溢出:是一种错误的发布,当一个对象还没有构造完成时,就使它被其他线程所见。 接下来,给出一个不安全的发布示例代码和对象溢出示例代码。
170 0
|
安全 Java 数据库连接
Hibernate中SessionFactory是线程安全的吗?Session是线程安全的吗(两个线程能够共享同一个Session吗)?
SessionFactory对应Hibernate的一个数据存储的概念,它是线程安全的,可以被多个线程并发访问。SessionFactory一般只会在启动的时候构建。
1396 0

热门文章

最新文章