C++ “for(;;)”比“while (TRUE)”快吗?如果不是,人们为什么要使用它?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2611246/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 00:04:32  来源:igfitidea点击:

Is "for(;;)" faster than "while (TRUE)"? If not, why do people use it?

c++coptimizationreadabilityinfinite-loop

提问by Chris Cooper

for (;;) {
    //Something to be done repeatedly
}

I have seen this sort of thing used a lot, but I think it is rather strange... Wouldn't it be much clearer to say while(true), or something along those lines?

我见过这种东西用得很多,但我觉得它很奇怪……说起来会不会更清楚while(true),或者类似的东西?

I'm guessing that (as is the reason for many-a-programmer to resort to cryptic code) this is a tiny margin faster?

我猜(这也是许多程序员求助于神秘代码的原因)这会快一点吗?

Why, and is it reallyworth it? If so, why not just define it this way:

为什么,真的值得吗?如果是这样,为什么不这样定义它:

#define while(true) for(;;)

See also: Which is faster: while(1) or while(2)?

另请参阅:哪个更快:while(1) 或 while(2)?

回答by Ben Zotto

  1. It's not faster.
  2. If you really care, compile with assembler output for your platform and look to see.
  3. It doesn't matter. This never matters. Write your infinite loops however you like.
  1. 它不是更快。
  2. 如果您真的很关心,请为您的平台使用汇编器输出进行编译并查看。
  3. 没关系。这从来都不重要。随心所欲地编写无限循环。

回答by jalf

I prefer for(;;)for two reasons.

我更喜欢for(;;)有两个原因。

One is that some compilers produce warnings on while(true)(something like "loop condition is constant"). Avoiding warnings is always a good thing to do.

一是一些编译器产生警告while(true)(类似于“循环条件是恒定的”)。避免警告总是一件好事。

Another is that I think for(;;)is clearer and more telling. I want an infinite loop. It literally hasno condition, it depends on nothing. I just want it to continue forever, until I do something to break out of it.

另一个是我认为for(;;)更清晰、更能说明问题的。我想要一个无限循环。它的字面没有条件,这取决于什么。我只是想让它永远持续下去,直到我做点什么来摆脱它。

Whereas with while(true), well, what's true got to do with anything? I'm not interested in looping until true becomes false, which is what this form literally says (loop while true is true). I just want to loop.

而与while(true),嗯,真正与任何事情有什么关系?我对循环不感兴趣,直到 true 变为 false,这就是这种形式的字面意思(在 true 为 true 时循环)。我只想循环。

And no, there is absolutely no performance difference.

不,绝对没有性能差异。

回答by ta.speot.is

Personally I use for (;;)because there aren't any numbers in it, it's just a keyword. I prefer it to while (true), while (1), while (42), while (!0)etc etc.

我个人使用,for (;;)因为它没有任何数字,它只是一个关键字。我喜欢它while (true)while (1)while (42)while (!0)等等等等。

回答by Mark Harrison

Because of Dennis Ritchie

因为丹尼斯·里奇

  • I started using for (;;)because that's the way Dennis Ritchie does it in K&R, and when learning a new language I always try to imitate the smart guys.

  • This is idiomatic C/C++. It's probably better in the long run to get used to it if you plan on doing much in the C/C++ space.

  • Your #definewon't work, since the thing being #define'd has to look like a C identifier.

  • All modern compilers will generate the same code for the two constructs.

  • 我开始使用for (;;)是因为丹尼斯·里奇 (Dennis Ritchie) 在 K&R 中就是这样做的,在学习一门新语言时,我总是尝试模仿聪明人。

  • 这是惯用的 C/C++。如果您计划在 C/C++ 领域做很多事情,从长远来看习惯它可能会更好。

  • #define不会工作,因为被#define'd 的东西必须看起来像一个 C 标识符。

  • 所有现代编译器都会为这两种结构生成相同的代码。

回答by Matthew Flaschen

It's certainly not faster in any sane compiler. They will both be compiled into unconditional jumps. The for version is easier to type (as Neil said) and will be clear if you understand for loop syntax.

在任何健全的编译器中它肯定不会更快。它们都将被编译成无条件跳转。for 版本更容易输入(正如 Neil 所说),如果您了解 for 循环语法,就会很清楚。

If you're curious, here is what gcc 4.4.1 gives me for x86. Both use the x86 JMPinstruction.

如果你很好奇,这里是 gcc 4.4.1 为我提供的 x86。两者都使用 x86 JMP指令。

void while_infinite()
{
    while(1)
    {
    puts("while");
    }
}

void for_infinite()
{
    for(;;)
    {
    puts("for");
    }
}

compiles to (in part):

编译为(部分):

.LC0:
.string "while"
.text
.globl while_infinite
    .type   while_infinite, @function
while_infinite:
    pushl   %ebp
    movl    %esp, %ebp
    subl    , %esp
.L2:
    movl    $.LC0, (%esp)
    call    puts
    jmp .L2
    .size   while_infinite, .-while_infinite
    .section    .rodata
.LC1:
    .string "for"
    .text
.globl for_infinite
    .type   for_infinite, @function
for_infinite:
    pushl   %ebp
    movl    %esp, %ebp
    subl    , %esp
.L5:
    movl    $.LC1, (%esp)
    call    puts
    jmp .L5
    .size   for_infinite, .-for_infinite

回答by Boann

I prefer for (;;)because it's the most consistent in different C-like languages.

我更喜欢for (;;)它,因为它在不同的类 C 语言中是最一致的。

In C++ while (true)is fine, but in C you depend on a header to define true, yet TRUEis a commonly used macro too. If you use while (1)it's correct in C and C++, and JavaScript, but not Java or C#, which require the loop condition to be a boolean, such as while (true)or while (1 == 1). In PHP, keywords are case-insensitive but the language prefers the capitalization TRUE.

在 C++while (true)中很好,但在 C 中你依赖于一个头文件来定义true,但TRUE也是一个常用的宏。如果while (1)在 C 和 C++ 以及 JavaScript 中使用它是正确的,但在 Java 或 C# 中是正确的,它们要求循环条件为布尔值,例如while (true)or while (1 == 1)。在 PHP 中,关键字不区分大小写,但该语言更喜欢大写TRUE

However, for (;;)is always completely correct in all of those languages.

但是,for (;;)在所有这些语言中总是完全正确的。

回答by Michael Burr

I personally prefer the for (;;)idiom (which will compile to the same code as while (TRUE).

我个人更喜欢for (;;)习惯用法(它将编译为与while (TRUE).

Using while (TRUE)may be more readable in one sense, I've decided to use the for (;;)idiom because it stands out.

while (TRUE)从某种意义上说,使用可能更具可读性,我决定使用这个for (;;)习语,因为它很突出

An infinite loop construct should be easily noticed or called out in code, and I personally think the for (;;)style does this a bit better than while (TRUE)or while (1).

一个无限循环结构应该很容易在代码中被注意到或调用,我个人认为这种for (;;)风格比while (TRUE)or好一点while (1)

Also, I recall that some compilers issue warnings when the controlling expression of a while loop is a constant. I don't think that happens too much, but just the potential for spurious warnings is enough for me to want to avoid it.

另外,我记得当 while 循环的控制表达式是一个常量时,一些编译器会发出警告。我认为这种情况不会发生太多,但只是虚假警告的可能性就足以让我想要避免它。

回答by rmeador

I've seen some people prefer it because they have a #define somewhere like this:

我见过有些人喜欢它,因为他们在这样的地方有一个#define:

#define EVER ;;

Which allows them to write this:

这允许他们写这个:

for (EVER)
{
    /* blah */
}

回答by bungle

What about (if your language supports it):

怎么样(如果您的语言支持):

start:
/* BLAH */
goto start;

回答by Jason Williams

There's no difference in terms of the machine code that is generated.

生成的机器代码没有区别。

However, just to buck the trend, I'd argue that the while(TRUE) form is much more readable and intuitive than for(;;), and that readability and clarity are much more important reasons for coding guidelines than any reasons I've heard for the for(;;) approach (I prefer to base my coding guidelines on solid reasoning and/or proof of effectiveness myself).

然而,为了逆势而上,我认为 while(TRUE) 形式比 for(;;) 更具可读性和直观性,并且可读性和清晰度是编码指南的重要原因,而不是我的任何原因我听说过 for(;;) 方法(我更喜欢将我的编码指南建立在可靠的推理和/或有效性证明的基础上)。