Java实例化 new和newInstance反射 性能对比测试

162 阅读7分钟

newInstance比new慢, 但某些情况下可忽略性能差异 new在十万次到二十亿次都是5毫秒左右不再增加, new基本没有等比增加耗时, 可能与虚拟机优化有关, 又或者是new实在太快了,十亿次以下都测不出准确耗时 单纯newInstance的性能的话, 用创建空String的实例来测可能比较真实

测试用例202206021331 AMD4800H64G

测试代码

package newInstance;

import java.lang.reflect.InvocationTargetException;

public class New和NewInstance速度对比2206021229 {
	
	static int sum;
	static void pln(Object o) {System.out.println(o);}
	
	public static class C2206021230{
		public  C2206021230(int a, int b) {
			sum += a + b;
		}
	}
	
	static int amount = 10_0000;
	
	static void test1() {
		long beginning = System.currentTimeMillis();
		
		for(int c=0; c<amount; c++) {
			new C2206021230(1, 2);
		}
		
		long t1 = System.currentTimeMillis();
		long cost1 = t1-beginning;
		pln("new C2206021230(1, 2) , "+amount+"次, 花费 "+cost1+" 毫秒");
		
		for(int c=0; c<amount; c++) {
			try {
				C2206021230.class.getDeclaredConstructor(int.class, int.class).newInstance(1, 2);
			} catch (InstantiationException | IllegalAccessException | IllegalArgumentException
					| InvocationTargetException | NoSuchMethodException | SecurityException e) {
				e.printStackTrace();
			}
		}
		long t2 = System.currentTimeMillis();
		long cost2 = t2-t1;
		pln("C2206021230.class.getDeclaredConstructor(int.class, int.class).newInstance(1, 2) , "+amount+"次, 花费 "+cost2+" 毫秒");
	}
	
	public static void main(String...arguments) {
		test1();
		pln("sum="+sum);
	}

}

一万次结果

new C2206021230(1, 2) , 10000次, 花费 1 毫秒
C2206021230.class.getDeclaredConstructor(int.class, int.class).newInstance(1, 2) , 10000次, 花费 14 毫秒
sum=60000

五万次结果

new C2206021230(1, 2) , 50000次, 花费 2 毫秒
C2206021230.class.getDeclaredConstructor(int.class, int.class).newInstance(1, 2) , 50000次, 花费 28 毫秒
sum=300000

十万次结果

new C2206021230(1, 2) , 100000次, 花费 4 毫秒
C2206021230.class.getDeclaredConstructor(int.class, int.class).newInstance(1, 2) , 100000次, 花费 28 毫秒
sum=600000

二十万次结果

new C2206021230(1, 2) , 200000次, 花费 4 毫秒
C2206021230.class.getDeclaredConstructor(int.class, int.class).newInstance(1, 2) , 200000次, 花费 39 毫秒
sum=1200000

三十万次结果

new C2206021230(1, 2) , 300000次, 花费 5 毫秒
C2206021230.class.getDeclaredConstructor(int.class, int.class).newInstance(1, 2) , 300000次, 花费 47 毫秒
sum=1800000

五十万次结果

new C2206021230(1, 2) , 500000次, 花费 5 毫秒
C2206021230.class.getDeclaredConstructor(int.class, int.class).newInstance(1, 2) , 500000次, 花费 50 毫秒
sum=3000000

一百万次结果

new C2206021230(1, 2) , 1000000次, 花费 4 毫秒
C2206021230.class.getDeclaredConstructor(int.class, int.class).newInstance(1, 2) , 1000000次, 花费 83 毫秒
sum=6000000

二百万次结果

new C2206021230(1, 2) , 2000000次, 花费 4 毫秒
C2206021230.class.getDeclaredConstructor(int.class, int.class).newInstance(1, 2) , 2000000次, 花费 98 毫秒
sum=12000000

五百万次结果

new C2206021230(1, 2) , 5000000次, 花费 4 毫秒
C2206021230.class.getDeclaredConstructor(int.class, int.class).newInstance(1, 2) , 5000000次, 花费 143 毫秒
sum=30000000

一千万次结果

new C2206021230(1, 2) , 10000000次, 花费 4 毫秒
C2206021230.class.getDeclaredConstructor(int.class, int.class).newInstance(1, 2) , 10000000次, 花费 218 毫秒
sum=60000000

三千万次结果

new C2206021230(1, 2) , 30000000次, 花费 6 毫秒
C2206021230.class.getDeclaredConstructor(int.class, int.class).newInstance(1, 2) , 30000000次, 花费 616 毫秒
sum=180000000

五千万次结果

new C2206021230(1, 2) , 50000000次, 花费 4 毫秒
C2206021230.class.getDeclaredConstructor(int.class, int.class).newInstance(1, 2) , 50000000次, 花费 780 毫秒
sum=300000000

一亿次结果

new C2206021230(1, 2) , 100000000次, 花费 5 毫秒
C2206021230.class.getDeclaredConstructor(int.class, int.class).newInstance(1, 2) , 100000000次, 花费 1382 毫秒
sum=600000000

两亿次结果

new C2206021230(1, 2) , 200000000次, 花费 5 毫秒
C2206021230.class.getDeclaredConstructor(int.class, int.class).newInstance(1, 2) , 200000000次, 花费 2543 毫秒
sum=1200000000

三亿次结果

new C2206021230(1, 2) , 300000000次, 花费 5 毫秒
C2206021230.class.getDeclaredConstructor(int.class, int.class).newInstance(1, 2) , 300000000次, 花费 3723 毫秒
sum=1800000000

四亿次结果

new C2206021230(1, 2) , 400000000次, 花费 5 毫秒
C2206021230.class.getDeclaredConstructor(int.class, int.class).newInstance(1, 2) , 400000000次, 花费 4868 毫秒
sum=-1894967296

五亿次结果

new C2206021230(1, 2) , 500000000次, 花费 5 毫秒
C2206021230.class.getDeclaredConstructor(int.class, int.class).newInstance(1, 2) , 500000000次, 花费 6063 毫秒
sum=-1294967296

十亿次结果

new C2206021230(1, 2) , 1000000000次, 花费 6 毫秒
C2206021230.class.getDeclaredConstructor(int.class, int.class).newInstance(1, 2) , 1000000000次, 花费 11685 毫秒
sum=1705032704

二十亿次结果

new C2206021230(1, 2) , 2000000000次, 花费 5 毫秒
C2206021230.class.getDeclaredConstructor(int.class, int.class).newInstance(1, 2) , 2000000000次, 花费 23242 毫秒
sum=-884901888

测试用例2206021931

测试代码

package newInstance;

import java.lang.reflect.InvocationTargetException;

public class New和NewInstance速度对比2206021916 {
	
	static long sum;
	static void pln(Object o) {System.out.println(o);}
	
	public static class C2206021230{
		public  C2206021230(int a, int b) {
			sum += a + b;
		}
	}
	
	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*2, 70*w, 80*w, 90*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,
			};
	
	static void test1(int amount) {
		long beginning = System.currentTimeMillis();
		
		for(int c=0; c<amount; c++) {
			new C2206021230(1, 2);
		}
		
		long t1 = System.currentTimeMillis();
		long cost1 = t1-beginning;
		
		for(int c=0; c<amount; c++) {
			try {
				C2206021230.class.getDeclaredConstructor(int.class, int.class).newInstance(1, 2);
			} catch (InstantiationException | IllegalAccessException | IllegalArgumentException
					| InvocationTargetException | NoSuchMethodException | SecurityException e) {
				e.printStackTrace();
			}
		}
		long t2 = System.currentTimeMillis();
		long cost2 = t2-t1;
		pln("执行 "+amount/10000+"万次, new用时 "+cost1 +" 毫秒     ,    newInstance用时 " + cost2+ "毫秒");
	}
	
	public static void main(String...arguments) {
		for(int amount : amountArray) {
			test1(amount);
		}
	}

}

测试结果

执行 1万次, new用时 1 毫秒 , newInstance用时 13毫秒 执行 1万次, new用时 0 毫秒 , newInstance用时 6毫秒 执行 1万次, new用时 0 毫秒 , newInstance用时 5毫秒 执行 1万次, new用时 0 毫秒 , newInstance用时 3毫秒 执行 1万次, new用时 0 毫秒 , newInstance用时 3毫秒 执行 1万次, new用时 0 毫秒 , newInstance用时 2毫秒 执行 1万次, new用时 0 毫秒 , newInstance用时 3毫秒 执行 2万次, new用时 1 毫秒 , newInstance用时 4毫秒 执行 3万次, new用时 1 毫秒 , newInstance用时 7毫秒 执行 4万次, new用时 0 毫秒 , newInstance用时 14毫秒 执行 5万次, new用时 1 毫秒 , newInstance用时 1毫秒 执行 6万次, new用时 0 毫秒 , newInstance用时 1毫秒 执行 7万次, new用时 0 毫秒 , newInstance用时 2毫秒 执行 8万次, new用时 0 毫秒 , newInstance用时 1毫秒 执行 9万次, new用时 0 毫秒 , newInstance用时 1毫秒 执行 1万次, new用时 0 毫秒 , newInstance用时 0毫秒 执行 2万次, new用时 0 毫秒 , newInstance用时 0毫秒 执行 3万次, new用时 1 毫秒 , newInstance用时 1毫秒 执行 4万次, new用时 0 毫秒 , newInstance用时 0毫秒 执行 5万次, new用时 0 毫秒 , newInstance用时 4毫秒 执行 6万次, new用时 0 毫秒 , newInstance用时 2毫秒 执行 7万次, new用时 0 毫秒 , newInstance用时 2毫秒 执行 8万次, new用时 1 毫秒 , newInstance用时 1毫秒 执行 9万次, new用时 1 毫秒 , newInstance用时 2毫秒 执行 10万次, new用时 0 毫秒 , newInstance用时 3毫秒 执行 20万次, new用时 1 毫秒 , newInstance用时 4毫秒 执行 30万次, new用时 0 毫秒 , newInstance用时 3毫秒 执行 40万次, new用时 0 毫秒 , newInstance用时 6毫秒 执行 50万次, new用时 0 毫秒 , newInstance用时 9毫秒 执行 0万次, new用时 0 毫秒 , newInstance用时 0毫秒 执行 70万次, new用时 0 毫秒 , newInstance用时 13毫秒 执行 80万次, new用时 0 毫秒 , newInstance用时 14毫秒 执行 90万次, new用时 3 毫秒 , newInstance用时 13毫秒 执行 100万次, new用时 5 毫秒 , newInstance用时 11毫秒 执行 200万次, new用时 1 毫秒 , newInstance用时 34毫秒 执行 300万次, new用时 0 毫秒 , newInstance用时 53毫秒 执行 400万次, new用时 1 毫秒 , newInstance用时 72毫秒 执行 500万次, new用时 1 毫秒 , newInstance用时 94毫秒 执行 600万次, new用时 2 毫秒 , newInstance用时 84毫秒 执行 700万次, new用时 2 毫秒 , newInstance用时 78毫秒 执行 800万次, new用时 2 毫秒 , newInstance用时 88毫秒 执行 900万次, new用时 2 毫秒 , newInstance用时 100毫秒 执行 1000万次, new用时 1 毫秒 , newInstance用时 110毫秒 执行 2000万次, new用时 4 毫秒 , newInstance用时 233毫秒 执行 3000万次, new用时 5 毫秒 , newInstance用时 334毫秒 执行 4000万次, new用时 7 毫秒 , newInstance用时 453毫秒 执行 5000万次, new用时 10 毫秒 , newInstance用时 562毫秒 执行 6000万次, new用时 11 毫秒 , newInstance用时 675毫秒 执行 7000万次, new用时 13 毫秒 , newInstance用时 777毫秒 执行 8000万次, new用时 15 毫秒 , newInstance用时 895毫秒 执行 9000万次, new用时 16 毫秒 , newInstance用时 1000毫秒 执行 10000万次, new用时 18 毫秒 , newInstance用时 1111毫秒 执行 20000万次, new用时 36 毫秒 , newInstance用时 2227毫秒 执行 30000万次, new用时 53 毫秒 , newInstance用时 3337毫秒

测试用例2206021933

测试代码

package newInstance;

import java.lang.reflect.InvocationTargetException;

public class New和NewInstance速度对比2206021933 {
	
	static long sum;
	static void pln(Object o) {System.out.println(o);}
	
	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*2, 70*w, 80*w, 90*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,
			};
	
	
	
	static void test1(int amount) {
		
		long beginning = System.currentTimeMillis();
		
		for(int c=0; c<amount; c++) {
			new String(String.valueOf(Math.random()));
		}
		
		long t1 = System.currentTimeMillis();
		long cost1 = t1-beginning;
		
		for(int c=0; c<amount; c++) {
			try {
				String.class.getDeclaredConstructor(String.class).newInstance(String.valueOf(Math.random()));
			} catch (InstantiationException | IllegalAccessException | IllegalArgumentException
					| InvocationTargetException | NoSuchMethodException | SecurityException e) {
				e.printStackTrace();
			}
		}
		long t2 = System.currentTimeMillis();
		long cost2 = t2-t1;
		pln("<tr><td>"+amount+"</td><td>"+amount/w+"万次</td><td>"+cost1+"</td><td>"+cost2+"</td></tr>");
	}
	
	public static void main(String...arguments) {
		pln("""
				<table><thead><tr>
					<th>执行次数</th><th>执行万次数</th><th>new 的用时(毫秒)</th><th>newInstance 的用时(毫秒)</th>
				</tr></thead><tbody>
				""");
		for(int amount : amountArray) {
			test1(amount);
		}
		pln("</tbody></table>");
	}

}

测试结果

执行次数执行万次数new 的用时(毫秒)newInstance 的用时(毫秒)
100001万次1029
100001万次412
100001万次210
100001万次28
100001万次24
100001万次24
100001万次24
200002万次39
300003万次612
400004万次614
500005万次815
600006万次69
700007万次737
800008万次2916
900009万次1218
100001万次12
200002万次44
300003万次47
400004万次67
500005万次79
600006万次811
700007万次912
800008万次913
900009万次1114
10000010万次1220
20000020万次2540
30000030万次3860
40000040万次5180
50000050万次63100
1200万次01
70000070万次88131
80000080万次95124
90000090万次106137
1000000100万次118154
2000000200万次235304
3000000300万次352460
4000000400万次471611
5000000500万次590765
6000000600万次704918
7000000700万次8211072
8000000800万次9361223
9000000900万次9881292
100000001000万次9831439
200000002000万次19682874
300000003000万次29534311
400000004000万次39435749
500000005000万次49277189
600000006000万次59138733
700000007000万次690510073
800000008000万次788611505
900000009000万次887212948
10000000010000万次992014394
20000000020000万次1975828889
30000000030000万次2961643465

测试用例2206021956

测试代码

package newInstance;

import java.lang.reflect.InvocationTargetException;

public class New和NewInstance速度对比2206021956 {
	
	static long sum;
	static void pln(Object o) {System.out.println(o);}
	
	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, 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,
			};
	
	
	
	static void test1(int amount) {
		
		long beginning = System.currentTimeMillis();
		
		for(int c=0; c<amount; c++) {
			new String();
		}
		
		long t1 = System.currentTimeMillis();
		long cost1 = t1-beginning;
		
		for(int c=0; c<amount; c++) {
			try {
				String.class.getDeclaredConstructor().newInstance();
			} catch (InstantiationException | IllegalAccessException | IllegalArgumentException
					| InvocationTargetException | NoSuchMethodException | SecurityException e) {
				e.printStackTrace();
			}
		}
		long t2 = System.currentTimeMillis();
		long cost2 = t2-t1;
		pln("<tr><td>"+amount+"</td><td>"+amount/w+"万次</td><td>"+cost1+"</td><td>"+cost2+"</td></tr>");
	}
	
	public static void main(String...arguments) {
		pln("""
				<table><thead><tr>
					<th>执行次数</th><th>执行万次数</th><th>new 的用时(毫秒)</th><th>newInstance 的用时(毫秒)</th>
				</tr></thead><tbody>
				""");
		for(int amount : amountArray) {
			test1(amount);
		}
		pln("</tbody></table>");
	}

}

测试结果

执行次数执行万次数new 的用时(毫秒)newInstance 的用时(毫秒)
100001万次014
100001万次010
100001万次16
100001万次05
100001万次03
100001万次03
100001万次03
200002万次06
300003万次010
400004万次010
500005万次111
600006万次016
700007万次03
800008万次13
900009万次14
100001万次02
200002万次01
300003万次00
400004万次02
500005万次02
600006万次02
700007万次04
800008万次06
900009万次06
10000010万次06
20000020万次012
30000030万次012
40000040万次013
50000050万次035
60000060万次039
70000070万次044
80000080万次052
90000090万次061
1000000100万次039
2000000200万次066
3000000300万次0100
4000000400万次0132
5000000500万次0167
6000000600万次0197
7000000700万次0232
8000000800万次1264
9000000900万次0295
100000001000万次0332
200000002000万次0660
300000003000万次0994
400000004000万次01327
500000005000万次01656
600000006000万次11985
700000007000万次12319
800000008000万次12665
900000009000万次22981
10000000010000万次13316
20000000020000万次46646
30000000030000万次49997
1000000000100000万次1533188