cout or printf which of the two has a faster execution speed C++?

后端 未结 15 2585
旧时难觅i
旧时难觅i 2020-11-27 07:04

I have been coding in C++ for a long time. I always wondered which has a faster execution speed printf or cout?

Situation: I am designing a

相关标签:
15条回答
  • 2020-11-27 07:16

    Performance is a non-issue for comparison; can't think of anything where it actually counts (developing a console-program). However, there's a few points you should take into account:

    • Iostreams use operator chaining instead of va_args. This means that your program can't crash because you passed the wrong number of arguments. This can happen with printf.

    • Iostreams use operator overloading instead of va_args -- this means your program can't crash because you passed an int and it was expecting a string. This can happen with printf.

    • Iostreams don't have native support for format strings (which is the major root cause of #1 and #2). This is generally a good thing, but sometimes they're useful. The Boost format library brings this functionality to Iostreams for those who need it with defined behavior (throws an exception) rather than undefined behavior (as is the case with printf). This currently falls outside the standard.

    • Iostreams, unlike their printf equivilants, can handle variable length buffers directly themselves instead of you being forced to deal with hardcoded cruft.

    Go for cout.

    0 讨论(0)
  • 2020-11-27 07:16

    To settle this:

    #include <iostream>
    #include <cstdio>
    #include <ctime>
    using namespace std;
    
    int main( int argc, char * argcv[] ) {
        const char * const s1 = "some text";
        const char * const s2 = "some more text";
        int x = 1, y = 2, z = 3;
        const int BIG = 2000;
        time_t now = time(0);
        for ( int i = 0; i < BIG; i++ ) {
            if ( argc == 1 ) {
                cout  << i << s1 << s2 << x << y << z << "\n";
            }
            else {
                printf( "%d%s%s%d%d%d\n", i, s1, s2, x, y, z );
            }
        }
        cout << (argc == 1 ? "cout " : "printf " ) << time(0) - now << endl;
    }
    

    produces identical timings for cout and printf.

    0 讨论(0)
  • 2020-11-27 07:18

    Another Stack Overflow question addressed the relative speed of C-style formatted I/O vs. C++ iostreams:

    • Why is snprintf faster than ostringstream or is it?

    • http://www.fastformat.org/performance.html

    Note, however, that the benchmarks discussed were for formatting to memory buffers. I'd guess that if you're actually performing the I/O to a console or file that the relative speed differences would be much smaller due to the I/O taking more of the overall time.

    0 讨论(0)
  • 2020-11-27 07:19

    If you're using C++, you should use cout instead as printf belongs to the C family of functions. There are many improvements made for cout that you may benefit from. As for speed, it isn't an issue as console I/O is going to be slow anyway.

    0 讨论(0)
  • 2020-11-27 07:21

    In practical terms I have always found printf to be faster than cout. But then again, cout does a lot more for you in terms of type safety. Also remember printf is a simple function whereas cout is an object based on a complex streams hierarchy, so it's not really fair to compare execution times.

    0 讨论(0)
  • 2020-11-27 07:22

    The reason C++ cout is slow is the default sync with stdio.

    Try executing the following to deactivate this issue.

    ios_base::sync_with_stdio(false)

    http://www.cplusplus.com/reference/iostream/ios_base/sync_with_stdio/

    http://msdn.microsoft.com/es-es/library/7yxhba01.aspx

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