其中循环有更好的表现?递增或递减? [英] Which loop has better performance? Increment or decrement?

查看:129
本文介绍了其中循环有更好的表现?递增或递减?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


  

可能重复:结果
  <一href=\"http://stackoverflow.com/questions/2823043/is-it-faster-to-count-down-than-it-is-to-count-up\">Is更快倒计时比它是计数?


该循环有更好的表现?我从一些地方的第二个好教训。但想知道为什么。

 的for(int i = 0; I&LT; = 10;我++)
      {
               /*这个更好 ?*/
      }
  对于(INT I = 10; I&GT; = 0;我 - )
      {
               /*这个更好 ?*/
      }


解决方案

第二个可能会更好,因为它更容易比较 I 0比比较 I 10,但我认为你可以使用其中的任何一个,因为编译器会对其进行优化。

Possible Duplicate:
Is it faster to count down than it is to count up?

which loop has better performance? I have learnt from some where that second is better. But want to know reason why.

  for(int i=0;i<=10;i++)
      {
               /*This is better ?*/
      }


  for(int i=10;i>=0;i--)
      {
               /*This is better ?*/
      }

解决方案

The second "may" be better, because it's easier to compare i with 0 than to compare i with 10 but I think you can use any one of these, because compiler will optimize them.

这篇关于其中循环有更好的表现?递增或递减?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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