Java通过Thread.currentThread().getStackTrace()[1].getClassName();获得当前类名速度性能测试

430 阅读3分钟

Java通过Thread.currentThread().getStackTrace()[1].getClassName();获得当前类名速度性能测试

测试用例2206022026 AMD4800H64G

测试代码

package stacktrace;

public class 通过currentThead的StackTrace获得ClassName速度测试2206022026 {
	
	static long sum;
	static void pln(Object...oar) {for(Object o:oar)System.out.println(o);}
	static String trt(String tag, Object...oar) {
		StringBuilder sb = new StringBuilder("<tr>");
		for(Object o:oar)sb.append("<"+tag+">"+o+"</"+tag+">");
		return (sb+"</tr>");
	}
	static String trth(Object...oar) {return trt("th",oar);}
	static String trtd(Object...oar) {return trt("td",oar);}
	
	static int w = 10000;
	static int amountArray[] = new int[] {
			w,w,w,w,w,w,
			w,20000,30000,40000,50000,60000,70000,80000,90000,
			w,20000,30000,40000,50000,60000,70000,80000,90000,
			10*w, 20*w, 30*w, 40*w, 50*w, 60*w, 70*w, 80*w, 90*w,
			100*w, 200*w, 300*w, 
//			100*w, 200*w, 300*w, 400*w, 500*w, 600*w, 700*w, 800*w, 900*w,
//			1000*w, 2000*w, 3000*w, 4000*w, 5000*w, 6000*w, 7000*w, 8000*w, 9000*w,
//			1*w*w, 2*w*w, 3*w*w,
//			10*w*w,
			};
	
	public static void main(int amount) {
		long t1 = System.currentTimeMillis();
		
		for(int c=0; c<amount; c++) {
			Thread.currentThread().getStackTrace()[1].getClassName();
		}
		
		long t2 = System.currentTimeMillis();
		long cost1 = t2-t1;
		
		pln(trtd(amount+"次",amount/10000+"万次",cost1+" 毫秒",cost1/1000D+" 秒"));
		
	}
	public static void main(String...arguments) {
		pln("<table><thead>"
				+ trth("执行次数","执行万次数","用时(毫秒)","用时(秒)")
				+ "</thead><tbody>");
		for(int amount:amountArray)main(amount);
		pln("</tbody></table>");
		
	}

}

测试结果

执行次数执行万次数用时(毫秒)用时(秒)
10000次1万次23 毫秒0.023 秒
10000次1万次19 毫秒0.019 秒
10000次1万次18 毫秒0.018 秒
10000次1万次18 毫秒0.018 秒
10000次1万次18 毫秒0.018 秒
10000次1万次18 毫秒0.018 秒
10000次1万次15 毫秒0.015 秒
20000次2万次30 毫秒0.03 秒
30000次3万次48 毫秒0.048 秒
40000次4万次65 毫秒0.065 秒
50000次5万次72 毫秒0.072 秒
60000次6万次83 毫秒0.083 秒
70000次7万次109 毫秒0.109 秒
80000次8万次126 毫秒0.126 秒
90000次9万次145 毫秒0.145 秒
10000次1万次15 毫秒0.015 秒
20000次2万次32 毫秒0.032 秒
30000次3万次49 毫秒0.049 秒
40000次4万次63 毫秒0.063 秒
50000次5万次80 毫秒0.08 秒
60000次6万次96 毫秒0.096 秒
70000次7万次113 毫秒0.113 秒
80000次8万次122 毫秒0.122 秒
90000次9万次125 毫秒0.125 秒
100000次10万次139 毫秒0.139 秒
200000次20万次277 毫秒0.277 秒
300000次30万次418 毫秒0.418 秒
400000次40万次547 毫秒0.547 秒
500000次50万次689 毫秒0.689 秒
600000次60万次819 毫秒0.819 秒
700000次70万次945 毫秒0.945 秒
800000次80万次1087 毫秒1.087 秒
900000次90万次1233 毫秒1.233 秒
1000000次100万次1357 毫秒1.357 秒
2000000次200万次2710 毫秒2.71 秒
3000000次300万次4092 毫秒4.092 秒