if语句vs if-else语句,哪个更快? [英] If statement vs if-else statement, which is faster?

查看:368
本文介绍了if语句vs if-else语句,哪个更快?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

前几天,我和一个朋友争论了这两个片段.哪个更快,为什么?

I argued with a friend the other day about those two snippets. Which is faster and why ?

value = 5;
if (condition) {
    value = 6;
}

和:

if (condition) {
    value = 6;
} else {
    value = 5;
}

如果value是矩阵怎么办?

注意:我知道value = condition ? 6 : 5;存在,我希望它会更快,但这不是一个选择.

Note: I know that value = condition ? 6 : 5; exists and I expect it to be faster, but it wasn't an option.

编辑(由于问题暂未解决,因此受到工作人员的要求):

Edit (requested by staff since question is on hold at the moment):

  • 请考虑采用优化和非优化版本的主流编译器(例如g ++,clang ++,vc,mingw )生成的 x86汇编 MIPS进行回答组装.
  • 当程序集有所不同时,请说明为什么版本会更快以及何时发布(例如,更好,因为没有分支且分支不会出现问题" )
  • please answer by considering either x86 assembly generated by mainstream compilers (say g++, clang++, vc, mingw) in both optimized and non optimized versions or MIPS assembly.
  • when assembly differ, explain why a version is faster and when (e.g. "better because no branching and branching has following issue blahblah")

推荐答案

TL; DR::在未优化的代码中,不带elseif似乎无关紧要,但即使是最基本的级别启用优化功能后,代码基本上会重写为value = condition + 5.

TL;DR: In unoptimized code, if without else seems irrelevantly more efficient but with even the most basic level of optimization enabled the code is basically rewritten to value = condition + 5.

尝试一下,并为以下代码生成了程序集:

I gave it a try and generated the assembly for the following code:

int ifonly(bool condition, int value)
{
    value = 5;
    if (condition) {
        value = 6;
    }
    return value;
}

int ifelse(bool condition, int value)
{
    if (condition) {
        value = 6;
    } else {
        value = 5;
    }
    return value;
}

在禁用优化功能(-O0)的gcc 6.3上,相关差异为:

On gcc 6.3 with optimizations disabled (-O0), the relevant difference is:

 mov     DWORD PTR [rbp-8], 5
 cmp     BYTE PTR [rbp-4], 0
 je      .L2
 mov     DWORD PTR [rbp-8], 6
.L2:
 mov     eax, DWORD PTR [rbp-8]

用于ifonly,而ifelse具有

 cmp     BYTE PTR [rbp-4], 0
 je      .L5
 mov     DWORD PTR [rbp-8], 6
 jmp     .L6
.L5:
 mov     DWORD PTR [rbp-8], 5
.L6:
 mov     eax, DWORD PTR [rbp-8]

后者看起来效率稍低,因为它有一个额外的跳动,但是两者都至少有两个任务,最多三个任务,因此除非您真的需要压缩每一个性能下降(提示:除非您正在乘坐航天飞机,否则,不会,即使那样,您可能也不),区别不会很明显.

The latter looks slightly less efficient because it has an extra jump but both have at least two and at most three assignments so unless you really need to squeeze every last drop of performance (hint: unless you are working on a space shuttle you don't, and even then you probably don't) the difference won't be noticeable.

但是,即使优化级别最低(-O1),这两个功能也会降低为相同的值:

However, even with the lowest optimization level (-O1) both functions reduce to the same:

test    dil, dil
setne   al
movzx   eax, al
add     eax, 5

基本上等同于

return 5 + condition;

假设condition为零或一. 更高的优化级别并没有真正改变输出,除非它们通过在开始时有效地将EAX寄存器清零来设法避免movzx.

assuming condition is zero or one. Higher optimization levels don't really change the output, except they manage to avoid the movzx by efficiently zeroing out the EAX register at the start.

免责声明:您可能不应该自己写5 + condition(即使标准保证将true转换为整数类型也会得到1),因为您的意图可能不会立即显现给阅读您的代码的人(可能包括您将来的自我).这段代码的目的是要证明两种情况下编译器产生的结果(实际上)是相同的. Ciprian Tomoiaga 在评论中说得很好:

Disclaimer: You probably shouldn't write 5 + condition yourself (even though the standard guarantees that converting true to an integer type gives 1) because your intent might not be immediately obvious to people reading your code (which may include your future self). The point of this code is to show that what the compiler produces in both cases is (practically) identical. Ciprian Tomoiaga states it quite well in the comments:

人类的工作是为人类编写代码,然后让编译器机器编写代码>.

a human's job is to write code for humans and let the compiler write code for the machine.

这篇关于if语句vs if-else语句,哪个更快?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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