测试分别在JDK 1.4.2和JDK 1.5中进行,但测试结果出乎我的意料.同样的代码,使用泛型后性能竟然有小部分的下降.
看来Sun还得加把劲,才能使JDK 1.5成为真正的“猛虎“.
/*
* 创建日期 2004-10-6
* 作者 2002PP
*/
package code.test;
import java.util.ArrayList;
import java.util.List;
/**
* @author 2002PP
* 在JDK 1.4.2下
*/
public class PerformanceTest
{
/** Creates a new instance of PerformanceTest */
public PerformanceTest()
{
}
public static void main(String[] args)
{
List list = new ArrayList();
long beforeAdd = System.currentTimeMillis();
int end = 100000;
for (int i = 0; i < end; i++)
{
list.add(new Integer(i));
}
long afterAdd = System.currentTimeMillis();
for (int j = 0; j < end; j++)
{
Integer integer = (Integer) list.get(j);
}
long afterGet = System.currentTimeMillis();
double addTime = (afterAdd - beforeAdd) / 1000.0d;
double getTime = (afterGet - afterAdd) / 1000.0d;
System.out.println(
"java.version=" + System.getProperty("java.version"));
System.out.println("Add耗时:" + addTime);
System.out.println("Get耗时:" + getTime);
}
}
结果:java.version=1.4.2
Add耗时:0.125
Get耗时:0.016
/*
* 创建日期 2004-10-6
* 作者 2002PP
*/
package code.test;
import java.util.ArrayList;
import java.util.List;
/**
* @author 2002PP
* 在JDK 1.5下
*/
public class PerformanceTest
{
/** Creates a new instance of PerformanceTest */
public PerformanceTest() {
}
public static void main(String[] args) {
List<Integer> list=new ArrayList<Integer>();
long beforeAdd=System.currentTimeMillis();
int end=100000;
for (int i=0;i<end;i++) {
list.add(new Integer(i));
}
long afterAdd=System.currentTimeMillis();
for (int j=0;j<end;j++) {
Integer integer=list.get(j);
}
long afterGet=System.currentTimeMillis();
System.out.println("java.version="+System.getProperty("java.version"));
double addTime=(afterAdd-beforeAdd) / 1000.0d;
double getTime=(afterGet-afterAdd) / 1000.0d;
System.out.println("Add耗时:"+addTime);
System.out.println("Get耗时:"+getTime);
}
}
结果:java.version=1.5.0
Add耗时:0.14
Get耗时:0.016
本文地址:http://com.8s8s.com/it/it14881.htm