Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
928 views
in Technique[技术] by (71.8m points)

java - What is better? if..else or multiple simple if

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 !

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

56.7k users

...