什么是更好的? if..else或多个简单if [英] What is better? if..else or multiple simple if

查看:97
本文介绍了什么是更好的? if..else或多个简单if的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

谈论java性能..什么更好? if..else或multiple simple if

  if(condition){
some_code;
返回值;
}
else if(condition){
some_code;
返回值;
}
else if(condition){
some_code;
返回值;
}
else {
some_code;
返回值;
}

  if(condition){
some_code;
返回值;
}

if(condition){
some_code;
返回值;
}

if(condition){
some_code;
返回值;
}

some_code;
返回值;

对您的想法感兴趣



Thnx!

解决方案

性能方面没有区别。选择最易读的选项,可能取决于代码的作用。



一般不要担心这些微优化。只有在您确定存在需要修复的性能问题后才能进行优化。


我们应该忘记效率低,大约97%的时间说:过早优化是所有邪恶的根源。 — Donald Knuth



talking about java performance .. what is better? if..else or multiple simple if

if( condition ) {
  some_code;
  return value;
}
else if( condition ) {
  some_code;
  return value;
}
else if( condition ) {
  some_code;
  return value;
}
else {
  some_code;
  return value;
}

or

if( condition ) {
  some_code;
  return value;
}

if( condition ) {
  some_code;
  return value;
}

if( condition ) {
  some_code;
  return value;
}

some_code;    
return value;

Interested in your thoughts

Thnx !

解决方案

There is no difference, performance-wise. Choose the most readable option, which could be either depending on what the code does.

In general do not worry about these micro-optimizations. Optimization should only come after you've determined there is a performance problem that needs to be fixed.

"We should forget about small efficiencies, say about 97% of the time: premature optimization is the root of all evil." —Donald Knuth

这篇关于什么是更好的? if..else或多个简单if的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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