简体   繁体   English

if和return或while最快和最合适的是什么?

[英]What is faster and best for with if and return or while?

I have a question about the for and while loops, as we have to travel a value until a condition is met. 我有一个关于for和while循环的问题,因为我们必须传递一个值直到满足条件。 I wonder which is more efficient at low level, and why? 我想知道哪种方法在较低级别更有效,为什么?

That is, these two codes give the same result: 也就是说,这两个代码给出相同的结果:

FOR: 对于:

for (int i = 0; i<10 ; i++)
{
    if (i==4)
    {
        return;
    }
}

WHILE: 而:

int i=0;
while (i<10 and i!=4)
{
    i++;
}

This is a small example of a possible loop, and we could be looking at a record of thousands. 这是一个可能循环的小例子,我们可能要看成千上万的记录。

What code is more effective? 哪种代码更有效? I've always said that I have to use a while in this case, but I wonder if a low level is still better while or better yet is for. 我一直说在这种情况下我必须使用一段时间,但是我想知道低水平是否会持续一段时间还是更好。

Thank you very much. 非常感谢你。

The answer is: it doesn't matter. 答案是:没关系。

You will not see any difference in performance in either, unless you really try hard to make code to see the difference, and what really matters is the readability of your code (and this is where you'll save time and and money in the future), so use whichever one is more understandable. 除非您真的努力使代码看到差异,否则您在性能上都不会看到任何差异,而真正重要的是代码的可读性(这是将来节省时间和金钱的地方) ),因此请使用更容易理解的一种。

In your case, i'll suggest the While approach ... 对于您的情况,我建议使用While方法...


I'll also suggest reading this article by Eric Lippert: How Bad Is Good Enough? 我还将建议阅读Eric Lippert的这篇文章: 足够好有多糟糕? , just in case you're not sold on the readability vs. silly optimizations :) ,以防万一您在可读性和愚蠢的优化上不满意:)

They should compile very similarly. 它们的编译应该非常相似。 At a low level you are looking at executing the commands within the loop, and then you will have two calls to compare a value and jump to the next block of code if the condition calls for exiting the loop. 从低层次看,您正在考虑在循环中执行命令,然后您将有两个调用以比较一个值,并在条件要求退出循环时跳转到下一个代码块。

As mentioned above, while should lead to better readability and thus is the better choice. 如上所述,虽然应该会导致更好的可读性,因此是更好的选择。

Both for and while are the same. forwhile都是相同的。

The only difference is where you place the condition. 唯一的区别是放置条件的位置。 internally the while loop uses the 'for' syntax in low level. 在内部,while循环在底层使用“ for”语法。

In your scenario. 在您的情况下。 While is the best option ** if you don't know the upper limit ** if you don't know the upper limit 虽然 **是最佳选择**

you can use 您可以使用

While(i!=4)
{
 i++
}

Use for loop if you know the upper limit, else while is the best friend. 使用for循环,如果你知道这个上限,否则while是最好的朋友。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 价值返回方法JPQL的最佳做法 - What best practices for value return method JPQL 在while循环中替换字符串的最佳方法是什么 - What is the best way to replace string in while loop 虽然循环保证返回一个值,什么应该替换return语句? - While loop guaranteed to return a value, what should replace return statement? 在rest API响应中返回登录凭据的最佳实践是什么? - What is the best practice to return login credentials in a rest API response? 使用Java泛型和工厂模式的返回类型的最佳实践是什么 - What Is Best Practice for Return Types using Java Generics and Factory Pattern 在 Spring 引导中返回每个 API 的统一响应的最佳方法是什么? - What is the best way to return the uniform response for each API in Spring Boot? 从Web服务返回大块二进制数据的最佳方法是什么? - What is the best way to return big chunks of binary data from a webservice? 从syncExec返回变量的最佳方法是什么? - What's the best way to return variables from a syncExec? 从码头服务器返回大文件的最佳方法是什么? - What is the best ways to return a big file from jetty server? 在 Spring Controller 中返回原始类型/包装器的最佳方法是什么? - What is the best way to return primitive types/wrappers in Spring Controller?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM