C++ cout printing slowly

前端 未结 8 2049
情深已故
情深已故 2020-11-30 14:23

I noticed if I print out a long string(char*) using cout it seems to print 1 character at a time to the screen in Windows 7, Vista, and Linux(using putty) using Visual C++ 2

相关标签:
8条回答
  • 2020-11-30 14:37

    I would suggest you try this same test on a different computer. I don't have a good answer for why this might be happening; all I can say is I have never noticed a speed difference between cout and printf. I also tested your code using gcc 4.3.2 on Linux and there was no difference whatsoever.

    That being said, you can't easily replace cout with your own implementation. The fact is, cout is an instance of std::ostream which has a lot of functionality built into it which is necessary for interoperability with other classes that overload the iostream operators.

    Edit:

    Anyone that says printf is always faster than std::cout is simply wrong. I just ran the test code posted by minjang, with gcc 4.3.2 and the -O2 flag on a 64-bit AMD Athlon X2, and cout was actually faster.

    I got the following results:

    printf: 00:00:12.024
    cout:   00:00:04.144
    

    Is cout always faster than printf? Probably not. Especially not with older implementations. But on newer implementations iostreams are likely to be faster than stdio because instead of parsing a format string at runtime, the compiler knows at compile time what functions it needs to call in order to convert integers/floats/objects to strings.

    But more importantly, the speed of printf versus cout depends on the implementation, and so the problem described by the OP is not easily explicable.

    0 讨论(0)
  • 2020-11-30 14:40

    Try call ios::sync_with_stdio(false); before using std::cout/cin, unless of course, you mix stdio and iostream in your program, which is a bad thing to do.

    0 讨论(0)
  • 2020-11-30 14:43

    Try using ios::sync_with_stdio(false);. Mention it before using std::cin/cout. It doesn't mix stdio or iostream but it synchronizes iostream standard streams with their corresponding standard c streams. for example - std::cin/wcin of iostream is synchronized with stdin of c stream

    0 讨论(0)
  • 2020-11-30 14:46

    Based on my experience in programming competitions, printf IS faster than cout.

    I remember many times when my solution didn't make it before the Time limit just because of cin/cout, while printf/scanf did work.

    Besides that, it seems normal (at least for me) that cout is slower than printf, because it does more operations.

    0 讨论(0)
  • 2020-11-30 14:52

    You should try to write all your data to an ostringstream first, and then use cout on the ostringstream's str(). I am on 64-bit Windows 7 and Test1 was already significantly faster than Test2 (your mileage may vary). Using an ostringstream to build a single string first and then using cout on that further decreased Test1's execution time by a factor of about 3 to 4. Be sure to #include <sstream>.

    I.e., replace

    void Test1()
    {
        cout
            << "a:" << a << "\n"
            << "a:" << setfill('0') << setw(8) << a << "\n"
            << "b:" << b << "\n"
            << "c:" << c << "\n"
            << "d:" << d << "\n"
            << "e:" << e << "\n"
            << "f:" << setprecision(6) << f << "\n"
            << "g:" << setprecision(10) << g << endl;
    }
    

    with:

    void Test1()
    {
        ostringstream oss;
        oss
            << "a:" << a << "\n"
            << "a:" << setfill('0') << setw(8) << a << "\n"
            << "b:" << b << "\n"
            << "c:" << c << "\n"
            << "d:" << d << "\n"
            << "e:" << e << "\n"
            << "f:" << setprecision(6) << f << "\n"
            << "g:" << setprecision(10) << g << endl;
        cout << oss.str();
    }
    

    I suspect ostringstream makes this so much faster as a result of not trying to write to the screen each time you call operator<< on cout. I've also noticed through experience that reducing the number of times you write to the screen (by writing more at once) increases performance (again, your mileage may vary).

    E.g.,

    void Foo1()
    {
        for(int i = 0; i < 10000; ++i) {
            cout << "Foo1\n";
        }
    }
    
    void Foo2()
    {
        std::string s;
        for(int i = 0; i < 10000; ++i) {
            s += "Foo2\n";
        }
        cout << s;
    }
    
    void Foo3()
    {
        std::ostringstream oss;
        for(int i = 0; i < 10000; ++i) {
            oss << "Foo3\n";
        }
        cout << oss.str();
    }
    

    In my case, Foo1 took 1,092ms, Foo2 took 234ms, and Foo3 took 218ms. ostingstreams are your friend. Obviously Foo2 and Foo3 require (trivially) more memory. To compare this against a C-style function, try sprintf into a buffer and then write that buffer using fprintf and you should see still more efficiency over Test2 (though for me this only improved performance of Test2 by about 10% or so; cout and printf are indeed different beasts under the hood).

    Compiler: MinGW64 (TDM and its bundled libraries).

    0 讨论(0)
  • 2020-11-30 14:55

    Try using some endls or flushes as they will flush cout's buffer, in case the OS is caching your program's output for whatever reason. But, as Charles says, there's no good explanation for this behavior, so if that doesn't help then it's likely a problem specific to your machine.

    0 讨论(0)
提交回复
热议问题