循环条件评估 [英] loop condition evaluation

查看:118
本文介绍了循环条件评估的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

只是一个快速问题。



我有一个循环,看起来像这样:

  for(int i = 0; i   



如果是这样,执行这样的操作会更有效率:

  int dimSquare = dim * dim; 
for(int i = 0; i

p>

- 已完成

解决方案

一般来说,值dim在你的循环,它会被重新评估每次。但是,因为在你的例子中不是这样,一个体面的编译器将优化你的代码,你不会看到任何性能的差异。


Just a quick question.

I have a loop that looks like this:

for (int i = 0; i < dim * dim; i++)

Is the condition in a for loop re-evaluated on every loop?

If so, would it be more efficient to do something like this?:

int dimSquare = dim * dim;
for (int i = 0; i < dimSquare; i++)

Thanks

-Faken

解决方案

In general, if you would for example change the value of "dim" inside your loop, it would be re-evaluated every time. But since that is not the case in your example, a decent compiler would optimize your code and you wouldn't see any difference in performance.

这篇关于循环条件评估的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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