package test;
import java.util.Calendar;
public class Test {
private static final String AAA = "aaa";
/**
* @param args
*/
public static void main(String[] args) {
long startTime;
long endTime;
System.out.println("Constant :");
startTime = Calendar.getInstance().getTimeInMillis();
for (int i = 0; i < 999999999; i++) {
String a = AAA;
}
endTime = Calendar.getInstance().getTimeInMillis();
System.out.println(endTime - startTime);
System.out.println();
System.out.println("Variable :");
startTime = Calendar.getInstance().getTimeInMillis();
for (int i = 0; i < 999999999; i++) {
String a = "a";
}
endTime = Calendar.getInstance().getTimeInMillis();
System.out.println(endTime - startTime);
}
}
结果
Constant :
1297
Variable :
1265
所以使用常量只是能提高代码重用性,并提高后期维护型,没有效率可言
阅读(1990) | 评论(0) | 转发(0) |