【蓝桥杯Java_C组·从零开始卷】第二节(附)、if与switch效率比较(千万次/一亿次)

2022-11-29 17:53:40 浏览数 (1)

前言:

分支数 小于三时,else if 效率更高 等于三时,效率近乎相同 大于三时,switch case效率更高

if与switch小于三次对比:

代码语言:javascript复制
package Action;

public class HelloWorld {
	static int count=10000000;
	public static void main(String[] args) {
		if_test();
		System.gc();//清理一下
		switch_test();
	}
	public static void if_test() {
		long start = System.currentTimeMillis();
		for (int i = 0; i <count; i  ) {
			if(i==(10000000-1)) {
				System.out.println("if判断一千万次");
			}
		}
		long end = System.currentTimeMillis();
		System.out.println("if判断一千万次" (end-start) "毫秒");
	}
	public static void switch_test() {
		long start = System.currentTimeMillis();
		for (int i = 0; i <count; i  ) {
			switch(i) {
				case 10000000-1:System.out.println("switch判断一千万次");break;
			}
		}
		long end = System.currentTimeMillis();
		System.out.println("switch判断一千万次" (end-start) "毫秒");
	}
}

区别还算明显吧,量少,可以用一亿试试。

代码语言:javascript复制
package Action;

public class HelloWorld {
	static int count=100000000;
	public static void main(String[] args) {
		if_test();
		System.gc();//清理一下
		switch_test();
	}
	public static void if_test() {
		long start = System.currentTimeMillis();
		for (int i = 0; i <count; i  ) {
			if(i==(100000000-1)) {
				System.out.println("if判断一亿次");
			}
		}
		long end = System.currentTimeMillis();
		System.out.println("if判断一亿次" (end-start) "毫秒");
	}
	public static void switch_test() {
		long start = System.currentTimeMillis();
		for (int i = 0; i <count; i  ) {
			switch(i) {
				case 100000000-1:System.out.println("switch判断一亿次");break;
			}
		}
		long end = System.currentTimeMillis();
		System.out.println("switch判断一亿次" (end-start) "毫秒");
	}
}

 这就很明显了。

if与switch等于三次对比:

代码语言:javascript复制
package Action;

public class HelloWorld {
	static int count=10000000;
	public static void main(String[] args) {
		switch_test();
		if_test();
	}
	public static void if_test() {
		long start = System.currentTimeMillis();
		for (int i = 0; i <count; i  ) {
			if(i==(10000000-1)) {
				System.out.println("if判断一千万次");
			}else if(i==-1) {
				
			}else if(i==-2){
				
			}
		}
		long end = System.currentTimeMillis();
		System.out.println("if判断一千万次" (end-start) "毫秒");
	}
	public static void switch_test() {
		long start = System.currentTimeMillis();
		for (int i = 0; i <count; i  ) {
			switch(i) {
				case 10000000-1:System.out.println("switch判断一千万次");break;
				case -1:break;
				case -2:break;
			}
		}
		long end = System.currentTimeMillis();
		System.out.println("switch判断一千万次" (end-start) "毫秒");
	}
}

三个的时候其实还是if快一些。

if与switch大于三次对比:

代码语言:javascript复制
package Action;

public class HelloWorld {
	static int count=10000000;
	public static void main(String[] args) {
		if_test();
		System.gc();
		switch_test();
	}
	public static void if_test() {
		long start = System.currentTimeMillis();
		for (int i = 0; i <count; i  ) {
			if(i==(10000000-1)) {
				System.out.println("if判断一千万次");
			}else if(i==-1) {
				
			}else if(i==-2){
				
			}else if(i==-3){
				
			}else if(i==-4){
				
			}else if(i==-5){
				
			}else if(i==-6){
				
			}else if(i==-7){
				
			}else if(i==-8){
				
			}else if(i==-9){
				
			}else if(i==-10){
				
			}
		}
		long end = System.currentTimeMillis();
		System.out.println("if判断一千万次" (end-start) "毫秒");
	}
	public static void switch_test() {
		long start = System.currentTimeMillis();
		for (int i = 0; i <count; i  ) {
			switch(i) {
				case 10000000-1:System.out.println("switch判断一千万次");break;
				case -1:break;
				case -2:break;
				case -3:break;
				case -4:break;
				case -5:break;
				case -6:break;
				case -7:break;
				case -8:break;
				case -9:break;
				case -10:break;
			}
		}
		long end = System.currentTimeMillis();
		System.out.println("switch判断一千万次" (end-start) "毫秒");
	}
}

这回效果还是比较明显的,用一亿再试试:

这就更明显了。

希望能对大家有所帮助。

0 人点赞