Java通过java.lang.SecurityManager的getClassContext()方法获取当前类的速度性能测试

刚发现 java.lang.SecurityManager 的好用之处就发现

@Deprecated(since="17", forRemoval=true)
public class SecurityManager {

从java17开始@Deprecated , 将要被移除
Why?Why?Why?
替代品是什么?

以下是对其获取当前类的性能的测试
测试代码👇

package securityManager;

public class GetClassContext速度测试2206021812 {
	static Class<?> cls=null;
	public static void test(int amount) {
		
		long beginning = System.currentTimeMillis();
		
		
		for(int c=0; c<amount; c++) {
			cls = new SecurityManager() {Class<?> n(){return this.getClassContext()[1];}}.n();
		}
		
		long t2 = System.currentTimeMillis();
		long cost1 = t2 - beginning;
		
		System.out.println("执行 "+amount/10000+" 万次,用时 "+ cost1 +" 毫秒");
	}
	
	static int w = 10000;
	static int iar[] = 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*2, 70*w, 80*w, 90*w,
			100*w, 200*w, 300*w
			};
	
	public static void main(String...arguments) {
		for(int max : iar)test(max);
	}

}

结果👇
执行 1 万次,用时 32 毫秒
执行 1 万次,用时 4 毫秒
执行 1 万次,用时 4 毫秒
执行 1 万次,用时 4 毫秒
执行 1 万次,用时 4 毫秒
执行 1 万次,用时 4 毫秒
执行 1 万次,用时 3 毫秒
执行 2 万次,用时 6 毫秒
执行 3 万次,用时 10 毫秒
执行 4 万次,用时 13 毫秒
执行 5 万次,用时 16 毫秒
执行 6 万次,用时 20 毫秒
执行 7 万次,用时 23 毫秒
执行 8 万次,用时 26 毫秒
执行 9 万次,用时 29 毫秒
执行 1 万次,用时 4 毫秒
执行 2 万次,用时 6 毫秒
执行 3 万次,用时 10 毫秒
执行 4 万次,用时 12 毫秒
执行 5 万次,用时 16 毫秒
执行 6 万次,用时 19 毫秒
执行 7 万次,用时 24 毫秒
执行 8 万次,用时 26 毫秒
执行 9 万次,用时 30 毫秒
执行 10 万次,用时 34 毫秒
执行 20 万次,用时 62 毫秒
执行 30 万次,用时 94 毫秒
执行 40 万次,用时 121 毫秒
执行 50 万次,用时 156 毫秒
执行 0 万次,用时 0 毫秒
执行 70 万次,用时 222 毫秒
执行 80 万次,用时 255 毫秒
执行 90 万次,用时 273 毫秒
执行 100 万次,用时 328 毫秒
执行 200 万次,用时 660 毫秒
执行 300 万次,用时 979 毫秒

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

kfepiza

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值