Is there any performance difference between looping twice separately and looping within a loop?
How to prove it or calculate it?
Is there any performance difference between looping twice separately and looping within a loop?
Share
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.
It depends entirely on the loops. Here are some examples of
O(n^2)running time:Here are some examples of
O(n)loops:Then you have the fact that better time complexities aren’t always faster for small enough
n, like the loop ton*n/50. Ifnis less than8(a positive int) then that loop won’t iterate at all, so it will obviously be faster than a the Simple loop withO(n), which will iterate exactlyntimes.