I wonder if C++ compilers will unroll range-based loop the same way they currently do for “normal” loops to maximize performance or in some case range-based loops will be slower than normal loops ?
Thank you very much.
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.
for range-based loop is equivalent to:
If compiler knows the number of iterations and it can solve the loop dependencies or loops are independent, then compiler is free to un-roll.
In general, loop unrolling is going to improve performance only for smaller loops. So, IMO, it does not matter whether range-based loops are unrolled or not. You can certainly benchmark with
-O3and-funroll-loopsand relevant options to see if there’s indeed any difference between two.