/**
* @(#)TestProfile.java
*
*
* @author
* @version 1.00 2009/8/18
*/
import java.util.Calendar;
public class TestStack {
private long x;
private static long staticX;
public TestStack() {
}
//显示时间
private long displayCurrentTime(String msg) {
Calendar crt = Calendar.getInstance();
System.out.println(msg);
System.out.println(crt.getTimeInMillis());
return crt.getTimeInMillis();
}
//成员变量测试
public void accessInstance(long val) {
long startTime = displayCurrentTime("成员变量开始:");
for (long i=0;i < val;i++) {
x += 1;
}
long endTime = displayCurrentTime("成员变量结束.");
System.out.println(new StringBuffer("成员变量测试总共花了:")
.append(endTime - startTime).append("毫秒.").toString());
}
//静态变量测试
public void accessStatic(long val) {
long startTime =displayCurrentTime("静态变量开始:");
for (long i=0;i < val;i++) {
TestStack.staticX += 1;
}
long endTime = displayCurrentTime("静态变量结束.");
System.out.println(new StringBuffer("静态变量测试总共花了:")
.append(endTime - startTime).append("毫秒.").toString());
}
public void accessStack(long val) {
long n = 0l;
long startTime = displayCurrentTime("局部变量(Stack)开始:");
for (long i=0;i < val;i++) {
n += 1;
}
long endTime = displayCurrentTime("局部变量(Stack)结束.");
System.out.println(new StringBuffer("局部变量(Stack)测试总共花了:")
.append(endTime - startTime).append("毫秒.").toString());
}
public static void main(String[] args) {
TestStack ts = new TestStack();
ts.accessStack(500000000);
ts.accessStatic(500000000);
ts.accessInstance(500000000);
}
}
局部变量(Stack)开始:
1250585378078
局部变量(Stack)结束.
1250585380125
局部变量(Stack)测试总共花了:2047毫秒.
静态变量开始:
1250585380125
静态变量结束.
1250585382515
静态变量测试总共花了:2390毫秒.
成员变量开始:
1250585382515
成员变量结束.
1250585385609
成员变量测试总共花了:3094毫秒.