美文网首页
直接创建对象和直接赋值方式创建对象性能比较

直接创建对象和直接赋值方式创建对象性能比较

作者: 罗正权 | 来源:发表于2016-03-25 17:22 被阅读242次
public class Test {
    
    public static void directSetValue(int times){
        BeTest test = new BeTest();
        for(int i=0;i<times;++i){
            test.setId(i);
            test.setName("dsfs");
            test.setName2("sdf");
            test.setName3("et");
        }
    }
    public static void createObject(int times){
        for(int i=0;i<times;++i){
            new BeTest(i,"dsfs","sdf","et");
        }
    }
    
    public static void cloneObject(int times){
        BeTest test = new BeTest(1,"dsfs","sdf","et");
        for(int i=0;i<times;++i){
            test.clone();
        }
    }
    
    public static void main(String args[]){
        int times=100000000;
        long current=System.currentTimeMillis();
        directSetValue(times);
        long useTime = System.currentTimeMillis()-current;
        current=System.currentTimeMillis();
        createObject(times);
        long useTime2 = System.currentTimeMillis()-current;
        current=System.currentTimeMillis();
        cloneObject(times);
        long useTime3 = System.currentTimeMillis()-current;
        System.err.println("复值方式构建对象:"+useTime+"\n直接创建对象:"+useTime2+
                "\n克隆方式创建对象:"+useTime3);
    }
}
class BeTest implements Cloneable{
    private Integer id;
    private String name;
    private String name2;
    private String name3;
    public void setId(Integer id) {
        this.id = id;
    }
    public void setName(String name) {
        this.name = name;
    }
    public void setName2(String name2) {
        this.name2 = name2;
    }
    public void setName3(String name3) {
        this.name3 = name3;
    }
    public BeTest() {
        super();
    }
    public BeTest(Integer id, String name, String name2, String name3) {
        super();
        this.id = id;
        this.name = name;
        this.name2 = name2;
        this.name3 = name3;
    }
    public Object clone(){
        Object t=null;
        try {
            t= super.clone();
        } catch (CloneNotSupportedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return t;
    }   
}

结果显示:
复值方式构建对象:481
直接创建对象:361
克隆方式创建对象:651
总结:如果不考虑内存,直接创建对象,比通过对象池方式或克隆对象方式获取对象更加高效

相关文章

  • 直接创建对象和直接赋值方式创建对象性能比较

    结果显示:复值方式构建对象:481直接创建对象:361克隆方式创建对象:651总结:如果不考虑内存,直接创建对象,...

  • 设计模式

    分类 创建型 创建对象时,不再直接实例化对象;而是根据特定场景,由程序来确定创建对象的方式,从而保证更高的性能、更...

  • 对 String 字符串的理解

    1、通过构造方法创建的字符串对象和直接赋值方式创建的字符串对象区别? 画图解释:捕获.PNG main 方法进栈开...

  • Java Script 日期对象

    一、创建日期对象的方式 1、构造函数:用来创建对象的方式,通过关键字new实现 2、字面量(直接量) 给变量赋值时...

  • 常用的对象字面量的增强写法

    对象的增强写法: 以前创建对象的写法:通过new一个对象实例。 现在创建对象直接用{ }赋值,这种写法很常见,那什...

  • Java工程师该如何编写高效代码?

    1.常量&变量 1.1.直接赋值常量值,禁止声明新对象 直接赋值常量值,只是创建了一个对象引用,而这个对象引用指向...

  • 对象的实例化

    创建对象的方式 对象创建的过程 对象的内存布局 对象的访问定位 hotspot使用直接指针 其他的方式:句柄

  • 数组,循环语句,定时器,闭包

    创建数组 面向对象的方式创建 直接创建(推荐使用,性能更高) 获取数组的成员数量(长度) 数组常用方法 1、获取数...

  • 工厂模式?代码怎样优化?

    直接创建对象 没有使用任何模式的情况下,直接创建对象的方式,代码如下, 创建第二个对象 这无疑写了很多重复代码,况...

  • JavaScript基础之创建对象

    JavaScript对象的创建 在JavaScript中创建一个对象有三种方式。可以通过对象直接量、关键字new和...

网友评论

      本文标题:直接创建对象和直接赋值方式创建对象性能比较

      本文链接:https://www.haomeiwen.com/subject/ethdlttx.html