for循环条件下的性能差异?

发布于 2024-07-26 06:48:17 字数 515 浏览 6 评论 0原文

我有一个简单的问题,主要是出于好奇而提出的。

这两行代码有什么区别? (在C++中)

for(int i = 0; i < N, N > 0; i++)

for(int i = 0; i < N && N > 0; i++)

条件的选择是完全任意的,我只是对 , 和 && 之间的差异感兴趣。

无论如何,我都不是编码的初学者,但我从来没有为逗号运算符烦恼过。

是否存在性能/行为差异或者纯粹是审美差异?

最后一点,我知道还有比条件运算符更大的性能问题需要解决,但我只是很好奇。 放纵我。

编辑 感谢您的回答。

事实证明,引发这个问题的代码按照我所描述的方式滥用了逗号运算符。 我想知道有什么区别以及为什么它不是 && 运算符,但它只是写错了。 我不认为它有什么问题,因为它工作得很好。 谢谢你帮我理清思路。

I have a simple question that I am posing mostly for my curiousity.

What are the differences between these two lines of code? (in C++)

for(int i = 0; i < N, N > 0; i++)

for(int i = 0; i < N && N > 0; i++)

The selection of the conditions is completely arbitrary, I'm just interested in the differences between , and &&.

I'm not a beginner to coding by any means, but I've never bothered with the comma operator.

Are there performance/behavior differences or is it purely aesthetic?

One last note, I know there are bigger performance fish to fry than a conditional operator, but I'm just curious. Indulge me.

Edit
Thanks for your answers.

It turns out the code that prompted this question had misused the comma operator in the way I've described. I wondered what the difference was and why it wasn't a && operator, but it was just written incorrectly. I didn't think anything was wrong with it because it worked just fine. Thanks for straightening me out.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

罪歌 2024-08-02 06:48:17

使用这样的逗号只会放弃第一个条件

逗号运算符的意思是“按此顺序运行这些语句,并取最后一个语句的值”。

Using a comma like that will simply discard the first condition.

The comma operator means "run these statements in this order, and take the value of the last one".

[旋木] 2024-08-02 06:48:17

虽然看起来很像,

for(int i = 0; i < N, N > 0; i++)

for(int i = 0; i < N && N > 0; i++) 

并不等同。

这是证据。

int main(int argc, char* argv[])
{
  int N = 10;
  int i = 5;

  int val = (N, i);
  cout << val << endl;
}

结果:

5

这意味着当确定循环何时退出时,它将使用 N > 。 0 。
如果N = 10,这意味着它将始终为真并且循环永远不会退出。

运行这个并查看证明。

int main(int argc, char* argv[])
{
  int N = 10;
  int i = 5;

  for(int i = 0; i < N, N > 0; i++){
     cout << val << endl;
  }
}

bash-2.05$ ./a.out                  
0                                   
1                                   
2                                   
3                                   
4                                   
5                                   
6                                   
7                                   
8                                   
9                                   
10                                  
11                                  
...
142
143
144
145
146
147
148
^C

如果 N 是在循环内不改变的常量或变量
那么你可以删除 N > 0 通过先检查一次来检查,即

if (N > 0){
  for (int i = 0; i < N; i++)
   ...
}

Although it looks like it,

for(int i = 0; i < N, N > 0; i++)

and

for(int i = 0; i < N && N > 0; i++) 

are not equivalent.

Here is the proof.

int main(int argc, char* argv[])
{
  int N = 10;
  int i = 5;

  int val = (N, i);
  cout << val << endl;
}

Result:

5

Which means that the when determining when the loop will exit it will use N > 0.
If N = 10, this means that it will always be true and the loop will never exit.

Run this and see the proof.

int main(int argc, char* argv[])
{
  int N = 10;
  int i = 5;

  for(int i = 0; i < N, N > 0; i++){
     cout << val << endl;
  }
}

bash-2.05$ ./a.out                  
0                                   
1                                   
2                                   
3                                   
4                                   
5                                   
6                                   
7                                   
8                                   
9                                   
10                                  
11                                  
...
142
143
144
145
146
147
148
^C

If N is a constant or variable that doesn't change inside the loop
then you could just remove the N > 0 check by checking it once first, i.e.

if (N > 0){
  for (int i = 0; i < N; i++)
   ...
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文