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 秒
Logo

技术共进,成长同行——讯飞AI开发者社区

更多推荐