Is the code
while(currentLine <= endLine)
{
// more code
currentLine++;
}
equivalent to
while(currentLine < endLine || currentLine == endLine)
{
// more code
currentLine++;
}
in terms of performance?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
The
<=operation typically compiles down into a single bytecode instructionif_icmpleorifle. How the JVM interprets that is typically machine-dependent, but most hardware has support to evaluate < and <= as a single instruction. Consequently, you should probably expect the performance for<=to be the same as<.The Java compiler can potentially rewrite the second code as the first, meaning that there will be no performance penalty. However, this is an implementation detail.
Generally speaking, don’t worry about these sorts of microoptimizations unless you are sure that they’re the reason for a performance bottleneck. It’s extremely rare that decisions like this will have a profound effect on program runtime.
Hope this helps!