'printf' vs. 'cout' in C++

后端 未结 16 1814
梦如初夏
梦如初夏 2020-11-22 07:04

What is the difference between printf() and cout in C++?

16条回答
  •  情书的邮戳
    2020-11-22 07:21

    People often claim that printf is much faster. This is largely a myth. I just tested it, with the following results:

    cout with only endl                     1461.310252 ms
    cout with only '\n'                      343.080217 ms
    printf with only '\n'                     90.295948 ms
    cout with string constant and endl      1892.975381 ms
    cout with string constant and '\n'       416.123446 ms
    printf with string constant and '\n'     472.073070 ms
    cout with some stuff and endl           3496.489748 ms
    cout with some stuff and '\n'           2638.272046 ms
    printf with some stuff and '\n'         2520.318314 ms
    

    Conclusion: if you want only newlines, use printf; otherwise, cout is almost as fast, or even faster. More details can be found on my blog.

    To be clear, I'm not trying to say that iostreams are always better than printf; I'm just trying to say that you should make an informed decision based on real data, not a wild guess based on some common, misleading assumption.

    Update: Here's the full code I used for testing. Compiled with g++ without any additional options (apart from -lrt for the timing).

    #include 
    #include 
    #include 
    
    class TimedSection {
        char const *d_name;
        timespec d_start;
        public:
            TimedSection(char const *name) :
                d_name(name)
            {
                clock_gettime(CLOCK_REALTIME, &d_start);
            }
            ~TimedSection() {
                timespec end;
                clock_gettime(CLOCK_REALTIME, &end);
                double duration = 1e3 * (end.tv_sec - d_start.tv_sec) +
                                  1e-6 * (end.tv_nsec - d_start.tv_nsec);
                std::cerr << d_name << '\t' << std::fixed << duration << " ms\n"; 
            }
    };
    
    int main() {
        const int iters = 10000000;
        char const *text = "01234567890123456789";
        {
            TimedSection s("cout with only endl");
            for (int i = 0; i < iters; ++i)
                std::cout << std::endl;
        }
        {
            TimedSection s("cout with only '\\n'");
            for (int i = 0; i < iters; ++i)
                std::cout << '\n';
        }
        {
            TimedSection s("printf with only '\\n'");
            for (int i = 0; i < iters; ++i)
                printf("\n");
        }
        {
            TimedSection s("cout with string constant and endl");
            for (int i = 0; i < iters; ++i)
                std::cout << "01234567890123456789" << std::endl;
        }
        {
            TimedSection s("cout with string constant and '\\n'");
            for (int i = 0; i < iters; ++i)
                std::cout << "01234567890123456789\n";
        }
        {
            TimedSection s("printf with string constant and '\\n'");
            for (int i = 0; i < iters; ++i)
                printf("01234567890123456789\n");
        }
        {
            TimedSection s("cout with some stuff and endl");
            for (int i = 0; i < iters; ++i)
                std::cout << text << "01234567890123456789" << i << std::endl;
        }
        {
            TimedSection s("cout with some stuff and '\\n'");
            for (int i = 0; i < iters; ++i)
                std::cout << text << "01234567890123456789" << i << '\n';
        }
        {
            TimedSection s("printf with some stuff and '\\n'");
            for (int i = 0; i < iters; ++i)
                printf("%s01234567890123456789%i\n", text, i);
        }
    }
    

提交回复
热议问题