1.

How Do You Optimize Loops And Conditional Statements?

Answer»

• Reduce the number of temporary objects being used,especially in loops.
• Use short-circuit Boolean operators INSTEAD of normal Boolean operators.
• Eliminate unnecessary repeated method calls from loops.
• Move loop invariants outside the loop.
• Perform the loop BACKWARDS (this actually performs SLIGHTLY faster than FORWARD loops do). [Actually it is converting the test to compare against 0 that makes the difference].
• It can help to copy slower-access vars to fast local vars if you are going to operate on them repeatedly, as in a loop.
• Use exception TERMINATED infinite loops for long loops.
• Whatever can be calculated outside of a loop should be calculated outside of the loop.
• For multidimensional arrays store a reference for the currently accessed row in a variable.
• Cache the size of the collection in a local variable to use in a loop instead of repeatedly calling collection.size().

• Reduce the number of temporary objects being used,especially in loops.
• Use short-circuit Boolean operators instead of normal Boolean operators.
• Eliminate unnecessary repeated method calls from loops.
• Move loop invariants outside the loop.
• Perform the loop backwards (this actually performs slightly faster than forward loops do). [Actually it is converting the test to compare against 0 that makes the difference].
• It can help to copy slower-access vars to fast local vars if you are going to operate on them repeatedly, as in a loop.
• Use exception terminated infinite loops for long loops.
• Whatever can be calculated outside of a loop should be calculated outside of the loop.
• For multidimensional arrays store a reference for the currently accessed row in a variable.
• Cache the size of the collection in a local variable to use in a loop instead of repeatedly calling collection.size().



Discussion

No Comment Found