0

I've run the following program which took my pc 94 seconds to execute (I have an i5 3.2 GHz cpu with 4 Gb ram) .

I think that 94 seconds for printing a million numbers is not fast. Is there a way to make it faster ?

Also why does the program end in 0.016 seconds when I don't cout my counter variable ? ( Does it really loop a million times in that time period of 0.016 seconds ? )

#include <iostream>

int main()
{
    for (int counter{1}; counter <= 1e6 ; ++counter)
{
    std::cout << counter << " ";
}

    return 0;
}
alienCY
  • 225
  • 2
  • 12
  • 1
    you may wanna take a look at this http://stackoverflow.com/questions/14573424/c-does-cout-statement-makes-code-slower – Sanjay-sopho Dec 11 '16 at 12:58
  • 2
    Can *you* print a million numbers in under 94 seconds? I think not :-) More seriously, yes, output takes time, especially massive update to a graphical terminal where you're basically updating the entire screen. And, yes, your computer probably *is* fast enough to go through a million iterations in a hundredth of a second. Testing it locally sees it execute in 0.008s. – paxdiablo Dec 11 '16 at 12:59
  • 1
    writing on a screen is quite slow process, that would be why. Compiler optimalizes loops like "for" so I don't think there's any way to make it faster, unless you want to do some dank magic. – Marek Chocholáček Dec 11 '16 at 13:00
  • 1
    Generally, you don't want to optimize user interfaces for performance beyond what user can actually perceive. User just cannot read a million of integers in 94 seconds, so the entire optimization enterprise is pointless. Separate your computing logic from presentation logic. Optimize computing if necessary. – Ivan Aksamentov - Drop Dec 11 '16 at 13:02
  • Thank you people ! I was just wondering if what was slowing donw the process was the cout. I saw some source code from in here some days ago which was a password cracker that was designed to try every combination possible which amazed me since it found a 5 characters long pass in 2.5 secs after trying 3.5 million different combinations ! ( Cool stuff :P ) – alienCY Dec 11 '16 at 13:14

1 Answers1

1

This question has nothing to do about "computing speed" nor about "making programs faster". Incrementing a counter hardly counts as computing, and the performance overhead of emitting text to a console (that is, invoking std::cout) overshadows the computational cost of incrementing a variable by several orders of magnitude.

So, this question is about the slowness of emitting text to a console.

Emitting text to a console is generally viewed as something that does not need to be terribly efficient, and for this reason it is kind of slow in most operating systems, under most scenarios. This is kind of an unfortunate situation, because software is generally emitting text to a console when we are developing it, so it slows down the development process, but it is not too bad, because software in production environments generally does not do that: the logs are written into text files, and this is considerably faster.

Also, keep in mind that the process of emitting text to a console involves auxiliary overhead factors such as scrolling: once your screen has filled up with text, every new line of text causes the console window to scroll. That's quite expensive, too.

So, wanna make your printing program run faster? Immediately after starting it, minimize the console window; then immediately restore it to its original dimensions; you will find that it will be done. Almost all of the overhead is in displaying text and scrolling the window.

Mike Nakis
  • 56,297
  • 11
  • 110
  • 142