http://cache.baidu.com/c?word=system%3B%2E%3Bcurrenttimemillis&url=http%3A//yingwuhahahaha%2Eicode%2Ecsdn%2Enet/post/2007/09/09/55061&p=882a934ec89e03b707be9b7f0f5ccf&user=baidu
本来是做java排序接口的一个程序,但为了测试运行时间,就调用了System.currentTimeMillis(),结果两次调用System.currentTimeMillis()时获得的数据一样,见下面程序。
解决方法:调用System.nanoTime()函数,获得纳秒数差。
import java.util.Vector;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.Random;
import java.util.Arrays;
public class A{
class NodeComparator implements Comparator{
public NodeComparator(){
}
public int compare(Object obj1, Object obj2){
return ((Node)obj1).getID()
.compareTo(((Node)obj2).getID());
}
public boolean equals(Object obj){
return super.equals(obj);
}
}
class Node{
private Integer id;
private String s1, s2, s3;
public Node(Integer id, String s1, String s2, String s3){
this.id = id;
this.s1 = s1;
this.s2 = s2;
this.s3 = s3;
}
public Integer getID(){
return id;
}
}
public void test(int size, boolean verbose){
Vector vec = new Vector(size * 2);
ArrayList list = new ArrayList(size * 2);
Random ran = new Random();
for(int i = 0;i < size;i++){
vec.add(new Node(new Integer(ran.nextInt(size)), "s1", "s2", "s3"));
list.add(new Node(new Integer(ran.nextInt(size)), "s1", "s2", "s3"));
}
Object[] array = vec.toArray();
long begin = System.nanoTime(),begin1=System.currentTimeMillis(); //计时开始,若都用System.currentTimeMillis()无法获得差
//不知为何
Arrays.sort(array, new NodeComparator());
long end = System.nanoTime()-begin,end1=System.currentTimeMillis()-begin1; //计时结束
if (verbose){
for(int i = 0, len = ((size > 100)?100:size);i < len;i++){
System.out.println("vec[" + i + "] == "
+ ((Node)vec.get(i)).getID().toString()
+ " array[" + i + "] == "
+ ((Node)array[i]).getID().toString());
}
}
System.out.println("rnCost " + end+" "+end1
+ " us(毫微秒-6) to sort " + size + " nodes from Vectorrn");
array = list.toArray();
begin = System.nanoTime(); //计时开始
Arrays.sort(array, new NodeComparator());
end = System.nanoTime()-begin; //计时结束
if (verbose){
for(int i = 0, len = ((size > 100)?100:size);i < len;i++){
System.out.println("vec[" + i + "] == "
+ ((Node)vec.get(i)).getID().toString()
+ " array[" + i + "] == "
+ ((Node)array[i]).getID().toString());
}
}
System.out.println("rnCost " +" "+end
+ " us(毫微秒) to sort " + size + " nodes from ArrayListrn");
}
//java A 1000000 true
public static void main(String[] args){
A a = new A();
a.test(Integer.parseInt("50"),
Boolean.valueOf("false").booleanValue());
}
}
本来是做java排序接口的一个程序,但为了测试运行时间,就调用了System.currentTimeMillis(),结果两次调用System.currentTimeMillis()时获得的数据一样,见下面程序。
解决方法:调用System.nanoTime()函数,获得纳秒数差。
import java.util.Vector;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.Random;
import java.util.Arrays;
public class A{
class NodeComparator implements Comparator{
public NodeComparator(){
}
public int compare(Object obj1, Object obj2){
return ((Node)obj1).getID()
.compareTo(((Node)obj2).getID());
}
public boolean equals(Object obj){
return super.equals(obj);
}
}
class Node{
private Integer id;
private String s1, s2, s3;
public Node(Integer id, String s1, String s2, String s3){
this.id = id;
this.s1 = s1;
this.s2 = s2;
this.s3 = s3;
}
public Integer getID(){
return id;
}
}
public void test(int size, boolean verbose){
Vector vec = new Vector(size * 2);
ArrayList list = new ArrayList(size * 2);
Random ran = new Random();
for(int i = 0;i < size;i++){
vec.add(new Node(new Integer(ran.nextInt(size)), "s1", "s2", "s3"));
list.add(new Node(new Integer(ran.nextInt(size)), "s1", "s2", "s3"));
}
Object[] array = vec.toArray();
long begin = System.nanoTime(),begin1=System.currentTimeMillis(); //计时开始,若都用System.currentTimeMillis()无法获得差
//不知为何
Arrays.sort(array, new NodeComparator());
long end = System.nanoTime()-begin,end1=System.currentTimeMillis()-begin1; //计时结束
if (verbose){
for(int i = 0, len = ((size > 100)?100:size);i < len;i++){
System.out.println("vec[" + i + "] == "
+ ((Node)vec.get(i)).getID().toString()
+ " array[" + i + "] == "
+ ((Node)array[i]).getID().toString());
}
}
System.out.println("rnCost " + end+" "+end1
+ " us(毫微秒-6) to sort " + size + " nodes from Vectorrn");
array = list.toArray();
begin = System.nanoTime(); //计时开始
Arrays.sort(array, new NodeComparator());
end = System.nanoTime()-begin; //计时结束
if (verbose){
for(int i = 0, len = ((size > 100)?100:size);i < len;i++){
System.out.println("vec[" + i + "] == "
+ ((Node)vec.get(i)).getID().toString()
+ " array[" + i + "] == "
+ ((Node)array[i]).getID().toString());
}
}
System.out.println("rnCost " +" "+end
+ " us(毫微秒) to sort " + size + " nodes from ArrayListrn");
}
//java A 1000000 true
public static void main(String[] args){
A a = new A();
a.test(Integer.parseInt("50"),
Boolean.valueOf("false").booleanValue());
}
}