if-else 和 switch-case 性能比较

测试逻辑很简单,使用 if-else 和 switch-case 判断10万次,统计花费时间。

int main(int argc, char*argv[])
{
    if (argc < 2) {
		std::cout << "usage:" << argv[0] << " {start_letter} " << std::endl;
		std::cout << "Like:" << argv[0] << " a" << std::endl;
		return -1;
	}

	volatile char t = *argv[1];
	if (t > 'z' || t < 'a') {
		std::cout << "start_letter need between 'a' and 'z'" << std::endl;
		return -1;
	}

	// test switch case and if else
	nanolog::initialize(nanolog::GuaranteedLogger(), "/home/library/log/nanolog_spdlog/", "switch_ifelse", 1);


	auto start0 = chrono::steady_clock::now();
    for (int i = 0; i < 100000; i++) {
		if (t == 'a') {} 
		else if (t == 'b') {}
		else if (t == 'c') {} 
		else if (t == 'd') {}
		else if (t == 'e') {} 
		else if (t == 'f') {}
		else if (t == 'g') {} 
		else if (t == 'h') {}
		else if (t == 'i') {} 
		else if (t == 'j') {} 
		else if (t == 'k') {} 
		else if (t == 'l') {} 
		else if (t == 'm') {} 
		else if (t == 'n') {} 
		else if (t == 'o') {} 
		else if (t == 'p') {} 
		else if (t == 'q') {} 
		else if (t == 'r') {} 
		else if (t == 's') {} 
		else if (t == 't') {} 
		else if (t == 'u') {} 
		else if (t == 'v') {} 
		else if (t == 'w') {} 
		else if (t == 'x') {} 
		else if (t == 'y') {} 
		else if (t == 'z') {} 
		else {}
    }

    auto end0 = chrono::steady_clock::now();
    auto diff0 = end0 - start0;
    std::cout << "if-else cost:" << chrono::duration <double, milli> (diff0).count() << " ms" << std::endl;

	auto start1 = chrono::steady_clock::now();
    for (int i = 0; i < 100000; i++) {
		switch (t) {
			case 'a':{break;}
			case 'b':{break;}
			case 'c':{break;}
			case 'd':{break;}
			case 'e':{break;}
			case 'f':{break;}
			case 'g':{break;}
			case 'h':{break;}
			case 'i':{break;}
			case 'j':{break;}
			case 'k':{break;}
			case 'l':{break;}
			case 'm':{break;}
			case 'n':{break;}
			case 'o':{break;}
			case 'p':{break;}
			case 'q':{break;}
			case 'r':{break;}
			case 's':{break;}
			case 't':{break;}
			case 'u':{break;}
			case 'v':{break;}
			case 'w':{break;}
			case 'x':{break;}
			case 'y':{break;}
			case 'z':{break;}
			default :{break;}
		}
    }

    auto end1 = chrono::steady_clock::now();
    auto diff1 = end1 - start1;
    std::cout << "switch-case cost:" << chrono::duration <double, milli> (diff1).count() << " ms" << std::endl;
}

测试结果如下:

# ./test a
if-else cost:0.252389 ms
switch-case cost:0.198403 ms
# ./test b
if-else cost:0.19388 ms
switch-case cost:0.194518 ms
# ./test c
if-else cost:0.23889 ms
switch-case cost:0.190069 ms
# ./test d
if-else cost:0.29482 ms
switch-case cost:0.192422 ms
# ./test e
if-else cost:0.240814 ms
switch-case cost:0.190581 ms
# ./test f
if-else cost:0.280842 ms
switch-case cost:0.197787 ms
# ./test g
if-else cost:0.302771 ms
switch-case cost:0.193035 ms
# ./test h
if-else cost:0.45625 ms
switch-case cost:0.193311 ms
# ./test i
if-else cost:0.437678 ms
switch-case cost:0.190678 ms
# ./test j
if-else cost:0.422324 ms
switch-case cost:0.193234 ms
# ./test k
if-else cost:0.458629 ms
switch-case cost:0.202947 ms
# ./test l
if-else cost:0.4849 ms
switch-case cost:0.194804 ms
# ./test m
if-else cost:0.543422 ms
switch-case cost:0.189874 ms
# ./test n
if-else cost:0.584596 ms
switch-case cost:0.212038 ms
# ./test o
if-else cost:0.61304 ms
switch-case cost:0.216401 ms
# ./test p
if-else cost:0.633069 ms
switch-case cost:0.196407 ms
# ./test q
if-else cost:0.698117 ms
switch-case cost:0.196405 ms
# ./test r
if-else cost:0.727131 ms
switch-case cost:0.193471 ms
# ./test s
if-else cost:0.783189 ms
switch-case cost:0.194345 ms
# ./test t
if-else cost:0.829187 ms
switch-case cost:0.196538 ms
# ./test u
if-else cost:0.870645 ms
switch-case cost:0.190033 ms
# ./test v
if-else cost:0.954029 ms
switch-case cost:0.192868 ms
# ./test w
if-else cost:1.69277 ms
switch-case cost:0.225514 ms
# ./test x
if-else cost:0.929775 ms
switch-case cost:0.189578 ms
# ./test y
if-else cost:0.97501 ms
switch-case cost:0.194859 ms
# ./test z
if-else cost:1.09059 ms
switch-case cost:0.202673 ms

可见,随着条件判断的增多,if-else 会逐渐变慢。最后,输入参数为 z 时,if -else 比 switch-case 多花费将近0.887917 毫秒 = 887.917 微妙 = 887917 纳秒,平均到一次判断 887917 / 100000 = 8 纳秒,对于常见的应用,大部分情况,判断条件应该没有测试中这么多,而且并非每次都是最差的情况,因此,一次判断的时间差应该会小于 8 纳秒。

对于低延迟应用,性能方面你们是否可以容忍? switch case 在程序编写方便,会显得更加清晰,简洁。

欢迎交流,指导。

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值