for循环VS while循环在编程语言,c ++ / java? [英] for loop VS while loop in programming languages, c++/java?

查看:185
本文介绍了for循环VS while循环在编程语言,c ++ / java?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

哪种性能更好?这可能与其他编程语言不一致,因此如果它们不同,或者您可以用特定语言的知识回答我的问题,请解释。



我将使用c ++作为例子,但我想知道它如何在java,c或任何其他主流语言中工作。

  int x = 0; 
while(x <10){
cout<< x<< \\\
;
x ++;
}

VS

  for(int x = 1; x <10; x ++)
cout< x<< \\\
;

哪一个更好?如果它是for循环,那么让我们说已经声明我们可以在while循环增量中使用的整数,我们不需要为while循环创建



示例:

  int age = //这是为代码中的其他东西,而不是一个while循环。但是幸运的是,我们的while循环只是这样发生需要数字17. 

while(age< 25){
cout<年龄< \\\
;
age ++;
}

这个实例会使while循环比创建一个for循环更好吗?我看到一些类似的问题,但我不相信这是一个重复,或任何答案在其他问题回答了我的问题。



我想要解释这个问题,解释如果它是编译器特定的,它是如何工作的,或者什么是这个问题的好回答。

解决方案<

我很难想象你给出的代码示例会有不同的性能特征。



我有一个小小的好奇心。在类似Pascal的语言(例如Delphi)中,循环限制仅被计算一次。这不同于C类语言,其中每次迭代评估循环限制。这可能会有性能影响,但是当然通过引入一个局部的外部循环以C语言编写高性能代码很简单。



例如:



Delphi

  for i:= 0 to List.Count -1 do 
DoStuff(List [i]);

List.Count 只计算一次。



C ++

  for i = 0; i  DoStuff(List.getItem(i)); 

这里, List.getCount()



如果发现评估循环限制是很昂贵的,那么这种差异可能是相关的。自然地,在循环外评估 List.getCount()并将结果存储在局部变量中是很简单的。



比较了Pascal和C / C ++的 for 循环,我会说Pascal版本在比较中非常简单。这不一定是坏事,因为更复杂的是总是,而可用。


Which is better for performance? This may not be consistent with other programming languages, so if they are different, or if you can answer my question with your knowledge in a particular language, please explain.

I will be using c++ as an example, but I would like to know how it works in java, c, or any other mainstream languages.

int x = 0;
 while (x < 10) {
    cout << x << "\n ";
    x++;
  }

VS

for ( int x = 1; x < 10; x++)   
 cout << x << "\n ";

Which one performs better? If it is the for loop, then lets say that there was an integer already declared that we could use in the while loop increment, that we didn't need to create just for the while loop?

example:

int age = 17; //this was made for something else in the code, not a while loop. But fortunately for us, our while loop just so happened to need the number 17.

 while (age < 25) {
        cout << age << "\n ";
        age++;
      }

Would this instance make the while loop a better choice than creating a for loop? And I have seen questions somewhat similar to this, but I do not believe that this is a duplicate, or any of the answers on those other questions answered my question.

I want an explanation on this question, explaining if it is compiler specific, how it works, or whatever is a good answer to this question.

解决方案

I find it hard to imagine situations where the code samples you give would have different performance characteristics.

I do have a mild curiosity for you though. In Pascal like languages (e.g. Delphi) the loop limits are evaluated only once. This differs from the C like languages where the loop limits are evaluated each iteration. This can have performance implications but of course its trivial to write performant code in C like languages by introducing a local outside the loop.

For example:

Delphi

for i := 0 to List.Count-1 do
  DoStuff(List[i]);

List.Count is only evaluated once.

C++

for (int i=0; i<List.getCount(); i++)
  DoStuff(List.getItem(i));

Here, List.getCount() is called every time around the loop.

If it transpires that evaluating the loop limits is expensive then this difference can be relevant. Naturally it is trivial to evaluate List.getCount() outside the loop and store the result in a local variable.

Having compared the for loops of Pascal and C/C++ I would say that the Pascal version is very simplistic in comparison. This is not necessarily a bad thing because for more complex there is always while available.

这篇关于for循环VS while循环在编程语言,c ++ / java?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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