4

given the following very simple for loop:

int main (void) {
    for (int i = 0 ; i < 1000000; i++) {
         std::cout<<i<<std::endl;
    }
}

running this code on a clean Windows 8 professional using Microsoft visual studio 2012 takes about 15 secs for every 100k prints.

On mac os x , using the same computer , it takes barely 3 secs for xcode to output 1 mill lines.

I'm almost 100% sure that it has nothing with the performance and it is just something that is related to the output mechanics or something.

Can someone confirm this..? just to know that my windows & visual studio are fine.

4
  • Visual output is usually slow, as there is nobody who could read as fast anyway. Try redirecting to file and see if then it's also so slow.
    – zch
    Jul 23, 2013 at 18:01
  • Yes, same for me (On windows/VS). But it has nothing to do with VS or XCode. You're not running your program in VS or XCode, do you? It's a speed problem of console display (cmd.exe) on Windows. If you redirect your output into a text file, it takes only 3-4 seconds on Windows (for me) to print the file. Jul 23, 2013 at 18:01
  • Yea, I knew it has nothing to do with the OS or IDE. thanks
    – Rouki
    Jul 23, 2013 at 18:02
  • On my MacOSX, it runs fine. Around 5 secs for 100k points... and barely the same on my windows 7 on the same computer ... What terminal do you use ? (I tried on different terminal, I compiled it with g++ 4.8.1 without any optimisation... ) Jul 23, 2013 at 18:07

3 Answers 3

3

This depends on external factors. Like the terminal application being used. For example, on OS X and Linux, you can bypass the terminal and run it with:

./program > /dev/null

It completes in about 0.2 seconds.

I/O in standard C++ is a blocking operation. That means the program "freezes" while it waits for the OS to process the output. In the end, if the terminal application isn't that fast, this will result in the program being "frozen" in a waiting state quite a lot.

2

std::endl flush the line. It is quite expensive to do this.

Try to do :

std::cout << i << '\n';

In most other usual interactive I/O scenarios, std::endl is redundant when used with std::cout because any input from std::cin, output to std::cerr, or program termination forces a call to std::cout.flush().

Use of std::endl in place of '\n', encouraged by some sources, may significantly degrade output performance.

Source


EDIT : Output operation are costly and depend on external factors. This is why it is slow here. For example, the terminal application being used can be factor of some performance issues.

You can avoid that by redirecting the output to /dev/null/ :

./a.out > /dev/null

On output performance, you can read this : http://codeforces.com/blog/entry/5217

3
  • For the record, std::endl is required to flush the line. Jul 23, 2013 at 18:04
  • endl is not the most expensive operation, cout is
    – wlyles
    Jul 23, 2013 at 18:14
  • @wlyles That is right. But the fact that endl forces the output immediatly is costly too. But here, it is correct to say that it is simply cout who makes this loop very slow. I will update my answer. Jul 23, 2013 at 18:33
1

Note that this is more speculation on my part, but still:

What I suspect is that the difference (btw. Windows / OSX) in overall runtime of you're little test-program hasn't got anything to do with the code produced by the respective compiler.

From my experience with console output on windows, I strongly suspect the "bottleneck" here is shoveling the character data from your program to the Windows Console and cmd.exe displaying it.

It could simply be that the console/shell/bash on OSX is much faster accepting the output of the program than the Windows Console.

What you can try is to redirect the output of this program to a file (by using redirection when starting it on the CLI test.exe > output.txt) and see whether you measure any difference this way.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.