package com
.zwk
.oop
;
public class newDemo {
public static void main(String
[] args
) {
test01();;
test02();
test03();
}
public static void test01(){
int time
= 1000000000;
long startTime
= System
.currentTimeMillis();
for (int i
= 0; i
< time
; i
++) {
User user
= new User();
user
.setName("张三");
user
.setAge(18);
user
.setSex("男");
}
long endTime
= System
.currentTimeMillis();
System
.out
.println("set方式:"+(endTime
-startTime
)+"ms");
}
public static void test02(){
int time
= 1000000000;
long startTime
= System
.currentTimeMillis();
for (int i
= 0; i
< time
; i
++) {
User user
= new User();
user
.born("张三",18,"男");
}
long endTime
= System
.currentTimeMillis();
System
.out
.println("init方式:"+(endTime
-startTime
)+"ms");
}
public static void test03(){
int time
= 1000000000;
long startTime
= System
.currentTimeMillis();
for (int i
= 0; i
< time
; i
++) {
User user
= new User("张三",18,"男");
}
long endTime
= System
.currentTimeMillis();
System
.out
.println("constrution方式:"+(endTime
-startTime
)+"ms");
}
}
class User{
private String name
;
private int age
;
private String sex
;
public User() {
}
public User(String name
, int age
, String sex
) {
this.name
= name
;
this.age
= age
;
this.sex
= sex
;
}
public User
born(String name
, int age
, String sex
){
this.name
= name
;
this.age
= age
;
this.sex
= sex
;
return this;
}
public String
getName() {
return name
;
}
public void setName(String name
) {
this.name
= name
;
}
public int getAge() {
return age
;
}
public void setAge(int age
) {
this.age
= age
;
}
public String
getSex() {
return sex
;
}
public void setSex(String sex
) {
this.sex
= sex
;
}
}
测试结果: set方式:18ms init方式:10ms constrution方式:9ms 以上结果,仅供参考,在多次测试中set方法是最耗时的一种。init方式和constrution方式耗时相对较少,推荐使用。 可给局部变量time 重新赋值 ,自己测试下试试!
转载请注明原文地址:https://ipadbbs.8miu.com/read-41467.html