Consider this simple problem:
Suppose I have a 1×4 array. I have to add 5 to each of its element. Then is it advisable to use a loop. Removing the size of code factor & good organization of code, is there any other reason why I should use a loop? Wont it take more time than executing 4 straight lines of code wherein I add 5 to each element as the control has to go back over 5 times & change the value of loop variable? What if we consider a 1×2 array? Then we dont even have the size problem, both types of code would consist of 2 lines.
Although I am tagging this question in C, I would like to know about this in other languages too.
You don’t really need to worry about this. Write the way you find it easier to read, then let the compiler decide whether it finds it necessary to perform some loop unrolling optimization. Trust compiler vendors, their developers are very good at understanding these kinds of optimization-related stuff…