的&QUOT性能上的差异;如果要是" VS"若否则如果" [英] Performance difference of "if if" vs "if else if"

查看:294
本文介绍了的&QUOT性能上的差异;如果要是" VS"若否则如果"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是在想有没有在C / C ++的2语句之间的性能差异:

案例1:

 如果(P == 0)
   做这个();
否则如果(第== 1)
   去做();
否则如果(第== 2)
   do_these():

案例2:

 如果(P == 0)
    做这个();
如果(第== 1)
    去做();
如果(第== 2)
    do_these();


解决方案

假设简单类型(在这种情况下,我用 INT ),没有搞笑的业务(不重新定义运算符=对于int),至少在AMD64 GCC 4.6,没有任何区别。生成的code是相同的:

  0000000000000000< case_1计算值:0000000000000040< case_2计算值:
   0:85 FF测试%EDI,EDI%40:85 FF测试%的EDI,电子数据交换%
   2:74 14 JE 18版; case_1 +为0x18> 42:74 14 JE 58 LT; case_2 +为0x18>
   4:83 FF 01 CMP $为0x1,%44 EDI:83 FF 01 CMP $为0x1,EDI%
   7:74 27 JE 30℃case_1 +的0x30> 47:74 27 JE 70℃,case_2 +的0x30>
   9:83 FF 02 CMP $ 0X2,EDI%49:83 FF 02 CMP $ 0X2,EDI%
   C:74 12 JE 20℃,case_1 + 0x20的> 4C:74 12 JE 60℃,case_2 + 0x20的>
   E:66 90 XCHG%斧,斧%4E:66 90 XCHG%斧,斧%
  10:F3 C3 REPZ retq 50:F3 C3 REPZ retq
  12:66 0F 44 1F 00 00 nopw为0x0(RAX%,%RAX,1)52:66 0F 44 1F 00 00 nopw为0x0(RAX%,%RAX,1)
  18:31 C0 XOR%EAX,EAX%58:31 C0 XOR%EAX,EAX%
  1A:E9 00 00 00 00 jmpq 1F< case_1 + 0x1F的> 5A:E9 00 00 00 00 jmpq 5F< case_2 + 0x1F的>
  1F:90 NOP 5F:90 NOP
  20:31 C0 XOR%EAX,EAX%60:31 C0 XOR%EAX,EAX%
  22:E9 00 00 00 00 jmpq 27所述; case_1 + 0×27> 62:E9 00 00 00 00 jmpq 67所述; case_2 + 0×27>
  27:66 0F 84 1F 00 00 00 nopw为0x0(RAX%,%RAX,1)67:66 0F 84 1F 00 00 00 nopw为0x0(RAX%,%RAX,1)
  2E:00 00 6E:00 00
  30:31 C0 XOR%EAX,EAX%70:31 C0 XOR%EAX,EAX%
  32:E9 00 00 00 00 jmpq 37℃; case_1 + 0x37符号> 72:E9 00 00 00 00 jmpq 77°; case_2 + 0x37符号>
  37:66 0F 84 1F 00 00 00 nopw为0x0(RAX%,%RAX,1)
  3E:00 00

在case_1末端的额外的指令仅仅是为填充(获取对准的下一个功能)

这是不是真的令人惊讶,搞清楚的P在该函数没有改变是相当基本的优化。如果p可以改变(例如,通过按引用或指针到各个 DO_ ... 函数,或者是一个引用或指针本身,所以有可能是一个别名)那么行为是不同的,当然产生code未免太。

I was just thinking is there any performance difference between the 2 statements in C/C++:

Case 1:

if (p==0)
   do_this();
else if (p==1)
   do_that();
else if (p==2)
   do_these():

Case 2:

if(p==0)
    do_this();
if(p==1)
    do_that();
if(p==2)
    do_these();

解决方案

Assuming simple types (in this case, I used int) and no funny business (didn't redefine operator= for int), at least with GCC 4.6 on AMD64, there is no difference. The generated code is identical:

0000000000000000 <case_1>:                                   0000000000000040 <case_2>:
   0:   85 ff                   test   %edi,%edi               40:   85 ff                   test   %edi,%edi
   2:   74 14                   je     18 <case_1+0x18>        42:   74 14                   je     58 <case_2+0x18>
   4:   83 ff 01                cmp    $0x1,%edi               44:   83 ff 01                cmp    $0x1,%edi
   7:   74 27                   je     30 <case_1+0x30>        47:   74 27                   je     70 <case_2+0x30>
   9:   83 ff 02                cmp    $0x2,%edi               49:   83 ff 02                cmp    $0x2,%edi
   c:   74 12                   je     20 <case_1+0x20>        4c:   74 12                   je     60 <case_2+0x20>
   e:   66 90                   xchg   %ax,%ax                 4e:   66 90                   xchg   %ax,%ax
  10:   f3 c3                   repz retq                      50:   f3 c3                   repz retq 
  12:   66 0f 1f 44 00 00       nopw   0x0(%rax,%rax,1)        52:   66 0f 1f 44 00 00       nopw   0x0(%rax,%rax,1)
  18:   31 c0                   xor    %eax,%eax               58:   31 c0                   xor    %eax,%eax
  1a:   e9 00 00 00 00          jmpq   1f <case_1+0x1f>        5a:   e9 00 00 00 00          jmpq   5f <case_2+0x1f>
  1f:   90                      nop                            5f:   90                      nop
  20:   31 c0                   xor    %eax,%eax               60:   31 c0                   xor    %eax,%eax
  22:   e9 00 00 00 00          jmpq   27 <case_1+0x27>        62:   e9 00 00 00 00          jmpq   67 <case_2+0x27>
  27:   66 0f 1f 84 00 00 00    nopw   0x0(%rax,%rax,1)        67:   66 0f 1f 84 00 00 00    nopw   0x0(%rax,%rax,1)
  2e:   00 00                                                  6e:   00 00 
  30:   31 c0                   xor    %eax,%eax               70:   31 c0                   xor    %eax,%eax
  32:   e9 00 00 00 00          jmpq   37 <case_1+0x37>        72:   e9 00 00 00 00          jmpq   77 <case_2+0x37>
  37:   66 0f 1f 84 00 00 00    nopw   0x0(%rax,%rax,1)
  3e:   00 00 

The extra instruction at the end of case_1 is just for padding (to get the next function aligned).

This isn't really surprising, figuring out that p isn't changed in that function is fairly basic optimization. If p could be changed (e.g., passed-by-reference or pointer to the various do_… functions, or was a reference or pointer itself, so there could be an alias) then the behavior is different, and of course the generated code would be too.

这篇关于的&QUOT性能上的差异;如果要是&QUOT; VS&QUOT;若否则如果&QUOT;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆