Skip to content

Loop and perfomance

If a computer does a million processes per second, how come for (int I = 0; I < 1000000; I++) {cout <<I;} takes forever?

compare

THIS program in the C or C++ language (for example):

for ( int i = 0 ; i < 1000000 ; i++ ) x += i ;

notice the NOT sending output to the display - that matters a LOT!
compiled it to machine code then the computer would have to:
1. Test to see if ‘i’ is still less than a million.
2. Add ‘i’ to ‘x’
3. Add one to ‘i’
4. Jump back to the beginning and do it all again.
This is about 4 machine code instructions - each done a million times - about 4 million instructions.
Because your computer can do BILLIONS of instructions per second it’ll go VERY fast…well under a millisecond on modern computers.
This presumes you’re using C or C++ and compiling the program to machine code.

versus

for ( int i = 0 ; i < 1000000 ; i++ ) cout << i ;

At first sight cout << i; looks no harder for the computer to do than “x += i ;” - but it is!
what the computer has to do in order to run one loop :
1. Test to see if ‘i’ is still less than a million.
2. Add one to ‘i’
3. Send ‘i’ to the standard output. Which entails:
4. Converting the number ‘i’ into a string of base-10 ASCII characters and adding a null terminator…requiring a fairly complicated function that has to check to see if the number is negative (it isn’t) then go around a loop which does something like dividing ‘i’ by 10 repeatedly and adding the ASCII code for ‘0’ to each digit. This is probably a few hundred machine code operations - more for bigger numbers fewer for smaller ones.
5. Sending the string of digits to the “standard output” stream - which involves another loop to copy each of those ASCII symbols into a buffer inside the cout stream object. A dozen more instructions.
6. Checking whether the buffer is full or not - which will probably happen every few hundred loops - and if it is…
7. Sending the buffer to the windowing system…HUNDREDS of instructions.
8. The windowing system will identify the on-screen window to which the buffer is to be sent.
9. Finding the font that is currently being used.
10. Checking to see if your string contains things like tabs, backspaces and newlines - or whether it’s trying to change to boldface or italics.
11. It may decide that the window has to be scrolled upwards - and will need to update the scroll bar and move all that other stuff up. Since you can scroll the window manually with the mouse - it also has to save your extra text someplace so you can scroll back to it later. (Millions of instructions!)
12. Then it’s making calls to the graphics device driver for your graphics card.
13. Which will result in operating system kernel calls that have to save the state of your program so it can return to it later (hundreds of instructions)…
14. That will probably cause it to look to see if any other program needs to run. - and maybe run those for a while (tens of millions of instructions!).
Eventually, the commands to draw the pixels that make up the symbols for the decimal value of ‘i’ are queued up to be sent to the GPU chip (probably many hundreds of instructions).
When the GPU is free (which may take a while if some other program is also drawing stuff) - then the data to update your window is sent to the GPU via the “DMA” mechanism when there is time.
Return from all of this and wait for the operating system to decide that it’s time for your program to run again…
And FINALLY…
Jump back to the beginning and do it all again.
So the thing is - while the loop itself still only took around a billionth of a second per loop - what you did inside the loop was to cause an enormous chain reaction of really, really complicated things to happen!

One very early lesson to learn when programming is that Input/Output is usually by FAR the most expensive thing a program ever does.

This isn’t the only possibility - you could maybe have redirected the standard output stream to a file on disk…which would have resulted in a whole other avalanche of processes and device drivers and stuff.

In fact, for things like video games - the entire process has to be cut short wherever possible by having the program talk more or less directly to the graphics chip - eliminating all but a couple of those steps.

Computers are insanely fast - but they do insanely complicated things!