Always avoid anything that can be done outside the loop, like method calls, assigning values ββto variables, or testing conditions.
A method call is more expensive than the equivalent code without a call, and repeating method calls over and over again, you simply add overhead to your application.
Move any method calls from the loop, even if you need to rewrite the code for this.
Benefits: -
If the compiler does not optimize it, the loop condition will be calculated for each iteration of the loop.
If the condition value does not change, the code will execute faster if the method call is removed from the loop.
Note: -
If the method returns a value that will not change during the cycle, store its value in a temporary variable until the cycle.
Therefore, its value is stored in the temporary size of the variable outside the loop, and then is used as a condition for ending the loop.
source share