What is a faster a loop that prints a statement to the screen 1000 times or 1000 print statements that each displays something..
I'm confused..
In my opinions 1000 print statements should be fast than a 1000 iterations of a loop.. because in a loop every time condition is checked increment process is done..
Here's a small program that demonstrates loop-unrolling with template meta-programming. The performance gain should become bigger as the loop count becomes greater.
The overhead of an increment, if and jump is going to be negligible compared to a single printf statement which has to interpret the format, build the output buffer, then flush the buffer to the screen. printf is very expensive.
koothkeeper wrote:
With 1000 print statements, you have to push arguments onto the stack, invoke the function, return from the function and adjust the stack.
Those arguments still have to be pushed onto the stack during each iteration of the loop.