Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

For or If-Else?  RSS feed

 
P. Sagdeo
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Anyone know what is faster: A for loop running 200 times or 200 if-else statements? Thanks!
 
Maarten Vergouwen
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not 100% sure how java handles these two instructions at the machine level, but in all 3GLs I've worked with If/Else is faster than a For-loop or something similar.
You should probably still use the For-loop however, since the speedloss is very marginal and readability and maintainability are much better.
 
Marilyn de Queiroz
Sheriff
Posts: 9080
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do you mean
if
else
if
else
if
else
if
else
if
else
if
else
if
else
if
else
if
else
if
else
if
else
.
.
.
???

What a mess!!
[ February 09, 2004: Message edited by: Marilyn de Queiroz ]
 
Sadanand Murthy
Ranch Hand
Posts: 382
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by P. Sagdeo:
Hi,
Anyone know what is faster: A for loop running 200 times or 200 if-else statements? Thanks!

Care to explain why you would want to write 200 if-else statments? What will you do if you realize that you need an additional 100?
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Moving this to the Performance forum...
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Maarten Vergouwen:
I'm not 100% sure how java handles these two instructions at the machine level, but in all 3GLs I've worked with If/Else is faster than a For-loop or something similar.

Yeah, but with Hotspot the for loop could actually be faster, as the more often some code gets executed, the more it will get optimized...
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This really is an apples vs. oranges comparison, I think. Loops and if/else statements do completely different things, and the speed of each one is very much dependent on what you do inside each of these constructs, which we have no information about. Can you show an example of what the code you're comparing would look like? What are you trying to do?
If you're thinking about having 200 if / else if statements, there's often a better way, say by using a switch statement, or doing some sort of lookup in an array, HashMap, or TreeMap. If you show a bit more of what you're doing, we can give better info on how this might work.
 
Mr. C Lamont Gilbert
Ranch Hand
Posts: 1170
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if-else is not repeatitive. It will not receive the same optimizations in any language that a for loop will.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!