退货的最佳方式是什么
如果我有一个返回某些内容的函数,那么最好的方法是什么,以下哪一项是最好的方法。是否遵循更好的格式,是否具有更好的性能?
private int myfunc()
{
int value=0;
for(int i=0;i<5;i++)
{
if (i==2)
value= 2;
}
return value;
}
或者
private int myfunc()
{
for(int i=0;i<5;i++)
{
if (i==2)
return(2);
}
return (0);
}
if i have a function that return something what is the best which one of the following is the best way to do it. does one follow better formatting, does one have better performance?
private int myfunc()
{
int value=0;
for(int i=0;i<5;i++)
{
if (i==2)
value= 2;
}
return value;
}
or
private int myfunc()
{
for(int i=0;i<5;i++)
{
if (i==2)
return(2);
}
return (0);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
我更喜欢第二种方法。
无论如何,如果您在找到所需的项目后通过“中断”循环来纠正第一种方法的实现,那么您不会注意到任何一种方法的性能差异。
不要声明不必要的变量是一个很好的编程习惯。从这方面来说,您在第二种方法中所做的事情是好的。第二种方法不会执行for循环中的所有循环。这是另一个好点。
如果您对第一种方法执行此操作。除了在第一种方法中声明不需要的变量之外,两者的执行几乎相同。第一种方法为垃圾收集器提供了第二种方法中不会发生的一些工作。
I prefer the second approach.
Anyway you won't notice any performance difference in either ways if you correct the implementation of the first approach by 'break'ing the loop once you find the item you want.
It's a good programming practice not to declare variables unnecessarily. What you have done in the second approach is good in that way. And the second approach does not execute all the cycles in the for loop. So another good point.
If you do this for the first approach. Both will perform almost the same except declaring unwanted variable in the first approach. First approach gives some work for the garbage collector that doesn't happen in the second approach.
这实际上处于“结构化编程”中一个古老的宗教问题的边缘:你应该让你的代码只有一个出口,还是可以有多个出口?
至少在 C/C++/Java 世界中,最常见的是一旦知道答案就返回。这对性能的影响非常小,但不像 20 年前那么严重。
This is actually on the edge of an old religious question in "structured programming": should you make your code have only one exit, or is it okay to have multiple exits?
In the C/C++/Java world, at least, it's most common to return as soon as you know the answer. There are very tiny performance implications for that, but not nearly as much as there were 20 years ago.
根据我个人的经验,第二个会更好,因为它会更快地到达返回语句。它不必循环遍历 i=3 到 i=5,因此运行速度会更快。当您只返回一个值而不是保留它一段时间时,也不会那么混乱。当您的代码变得更加复杂时,这很重要。
In my personal experience, the second one would be better because it will get to the return statement faster. It won't have to loop through i=3 through i=5, so it will run faster. It is also a little bit less confusing when you just return a value instead of keeping it for a while. This is important for when your code gets more complicated.
第二种方法不能按原样编译,您必须添加:
作为该方法的最后一行。
也就是说,第二种方法应该稍微快一些,因为执行的代码行更少。
作为一般规则,尝试将方法中的 return 语句的数量限制为最多 6 个。
请注意,编译器被允许并且可能确实将第一个方法转换为字节码中的第二个方法(变量
value
在循环之后不使用,因此允许编译器简单地返回它)。The second approach does not compile as it is, you must add:
as the last line of the method.
That said, the second method should be slightly faster, because there are less lines of code executed.
As a general rule, try to limit the number of return statements in method to a maximum of 6.
Note that the compiler is allowed to, and probably does, turn the first method into the second method in byte code (the variable
value
is not used after the loop, so the compiler is allowed to simply return it).正如上面用户所说的add return 0;这样IDE就会知道程序是否成功运行。
As the above user said add return 0; so that the IDE will know if the program ran successively.
如果您希望代码变得纯粹,您会喜欢
第一种方法
。原因之一是
continue
、break
并不是编写代码的好选择。始终可以在不使用continue
& 的情况下编写代码。中断
。将您的注意力集中在代码上,了解哪个部分到底在做什么以及它的范围是什么。从块返回也是可能的,但这与使用
continue
& 是一样的。中断
。从区块中
返回
当然也不错。但从方法结束返回是很好的做法。If you like to become purist with your code, you will like
first approach
.One reason is
continue
,break
is not a good choice to write code. It is always possible to write code without usingcontinue
&break
. Keeping your focus in the code regarding which part is doing exactly what and what is its scope.Returning from a block is also possible, but it is same thing like using of
continue
&break
.Surely its not bad to
return
from a block. But returning from end of the method is good practice.