排除"Else"子句是否有性能差异? [英] Is there a performance difference by excluding an 'Else' clause?

查看:36
本文介绍了排除"Else"子句是否有性能差异?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下两段代码之间是否存在性能差异?

Is there a performance difference between the following two pieces of code?

if (myCondition)
{
     return "returnVal1";
}

return "returnVal2"

if (myCondition)
{
     return "returnVal1";
}
else
{
     return "returnVal2";
}

我的直觉是编译器应该对此进行优化,并且应该没有区别,但是我经常在整个代码中看到它同时完成了两种工作.我想知道是否归结为偏好和可读性.

My gut feeling is that the compiler should optimize for this and there shouldn't be a difference, but I frequently see it done both ways throughout our code. I'd like to know if it comes down to a matter of preference and readability.

推荐答案

找出答案的最佳方法是看代码!这是VS2005 C#在发布模式下生成的代码:

The best way to find out is to look at the code! Here's the code the VS2005 C# produced in release mode:

    static bool F1 (int condition)
    {
      if (condition > 100)
00000000  push        ebp  
00000001  mov         ebp,esp 
00000003  push        eax  
00000004  mov         dword ptr [ebp-4],ecx 
00000007  cmp         dword ptr ds:[009185C8h],0 
0000000e  je          00000015 
00000010  call        79469149 
00000015  cmp         dword ptr [ebp-4],64h 
00000019  jle         00000024 
      {
        return true;
0000001b  mov         eax,1 
00000020  mov         esp,ebp 
00000022  pop         ebp  
00000023  ret              
      }

      return false;
00000024  xor         eax,eax 
00000026  mov         esp,ebp 
00000028  pop         ebp  
00000029  ret              
            }

    static bool F2 (int condition)
    {
      if (condition > 100)
00000000  push        ebp  
00000001  mov         ebp,esp 
00000003  push        eax  
00000004  mov         dword ptr [ebp-4],ecx 
00000007  cmp         dword ptr ds:[009185C8h],0 
0000000e  je          00000015 
00000010  call        79469109 
00000015  cmp         dword ptr [ebp-4],64h 
00000019  jle         00000024 
      {
        return true;
0000001b  mov         eax,1 
00000020  mov         esp,ebp 
00000022  pop         ebp  
00000023  ret              
      }
      else
      {
        return false;
00000024  xor         eax,eax 
00000026  mov         esp,ebp 
00000028  pop         ebp  
00000029  ret              
            }

这将显示两个版本产生的代码完全相同,正如您所希望的那样.我还尝试了第三个选项:

Which shows the two version produce the exact same code, as you would hope for. I also tried a third option:

    static bool F3 (int condition)
    {
      return condition > 100;
00000000  push        ebp  
00000001  mov         ebp,esp 
00000003  push        eax  
00000004  mov         dword ptr [ebp-4],ecx 
00000007  cmp         dword ptr ds:[009185C8h],0 
0000000e  je          00000015 
00000010  call        794690C9 
00000015  cmp         dword ptr [ebp-4],64h 
00000019  setg        al   
0000001c  movzx       eax,al 
0000001f  mov         esp,ebp 
00000021  pop         ebp  
00000022  ret              
            }

效率更高,因为它从不分支(分支通常是坏的!).

which is far more efficient as it never branches (and branches are usually bad!).

编辑

实际上,找出哪种方法更有效的最佳方法是分析代码,而不是查看汇编器.

Actually, the best way to find out which is more efficient is to profile the code, not look at the assembler.

此外,它产生的代码非常不寻常.push eax/mov [],ecx位肯定与单个push ecx相同.同样,它通过寄存器传递,然后将值存储在堆栈中.我想知道是否在调试器中运行代码以查看汇编程序是否正在更改代码的生成方式.

Also, the code it's produced is quite unusual. The push eax / mov [],ecx bit is the same, surely, as a single push ecx. Also, it passes by register then stores the value on the stack. I wonder if running the code in the debugger to look at the assembler is changing the way the code is generated.

这篇关于排除"Else"子句是否有性能差异?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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