Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 503261
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T06:22:34+00:00 2026-05-13T06:22:34+00:00

After performing some tests I noticed that printf is much faster than cout .

  • 0

After performing some tests I noticed that printf is much faster than cout. I know that it’s implementation dependent, but on my Linux box printf is 8x faster. So my idea is to mix the two printing methods: I want to use cout for simple prints, and I plan to use printf for producing huge outputs (typically in a loop). I think it’s safe to do as long as I don’t forget to flush before switching to the other method:

cout << "Hello" << endl;
cout.flush();

for (int i=0; i<1000000; ++i) {
    printf("World!\n");
}
fflush(stdout);

cout << "last line" << endl;
cout << flush;

Is it OK like that?

Update: Thanks for all the precious feedbacks. Summary of the answers: if you want to avoid tricky solutions, simply stick with cout but don’t use endl since it flushes the buffer implicitly (slowing the process down). Use "\n" instead. It can be interesting if you produce large outputs.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T06:22:34+00:00Added an answer on May 13, 2026 at 6:22 am

    The direct answer is that yes, that’s okay.

    A lot of people have thrown around various ideas of how to improve speed, but there seems to be quite a bit of disagreement over which is most effective. I decided to write a quick test program to get at least some idea of which techniques did what.

    #include <iostream>
    #include <string>
    #include <sstream>
    #include <time.h>
    #include <iomanip>
    #include <algorithm>
    #include <iterator>
    #include <stdio.h>
    
    char fmt[] = "%s\n";
    static const int count = 3000000;
    static char const *const string = "This is a string.";
    static std::string s = std::string(string) + "\n";
    
    void show_time(void (*f)(), char const *caption) { 
        clock_t start = clock();
        f();
        clock_t ticks = clock()-start;
        std::cerr << std::setw(30) << caption 
            << ": " 
            << (double)ticks/CLOCKS_PER_SEC << "\n";
    }
    
    void use_printf() {
        for (int i=0; i<count; i++)
            printf(fmt, string);
    }
    
    void use_puts() {
        for (int i=0; i<count; i++) 
            puts(string);        
    }
    
    void use_cout() { 
        for (int i=0; i<count; i++)
            std::cout << string << "\n";
    }
    
    void use_cout_unsync() { 
        std::cout.sync_with_stdio(false);
        for (int i=0; i<count; i++)
            std::cout << string << "\n";
        std::cout.sync_with_stdio(true);
    }
    
    void use_stringstream() { 
        std::stringstream temp;
        for (int i=0; i<count; i++)
            temp << string << "\n";
        std::cout << temp.str();
    }
    
    void use_endl() { 
        for (int i=0; i<count; i++)
            std::cout << string << std::endl;
    }
    
    void use_fill_n() { 
        std::fill_n(std::ostream_iterator<char const *>(std::cout, "\n"), count, string);
    }
    
    void use_write() {
        for (int i = 0; i < count; i++)
            std::cout.write(s.data(), s.size());
    }
    
    int main() { 
        show_time(use_printf, "Time using printf");
        show_time(use_puts, "Time using puts");
        show_time(use_cout, "Time using cout (synced)");
        show_time(use_cout_unsync, "Time using cout (un-synced)");
        show_time(use_stringstream, "Time using stringstream");
        show_time(use_endl, "Time using endl");
        show_time(use_fill_n, "Time using fill_n");
        show_time(use_write, "Time using write");
        return 0;
    }
    

    I ran this on Windows after compiling with VC++ 2013 (both x86 and x64 versions). Output from one run (with output redirected to a disk file) looked like this:

              Time using printf: 0.953
                Time using puts: 0.567
       Time using cout (synced): 0.736
    Time using cout (un-synced): 0.714
        Time using stringstream: 0.725
                Time using endl: 20.097
              Time using fill_n: 0.749
               Time using write: 0.499
    

    As expected, results vary, but there are a few points I found interesting:

    1. printf/puts are much faster than cout when writing to the NUL device
      • but cout keeps up quite nicely when writing to a real file
    2. Quite a few proposed optimizations accomplish little
      • In my testing, fill_n is about as fast as anything else
    3. By far the biggest optimization is avoiding endl
    4. cout.write gave the fastest time (though probably not by a significant margin

    I’ve recently edited the code to force a call to printf. Anders Kaseorg was kind enough to point out–that g++ recognizes the specific sequence printf("%s\n", foo); is equivalent to puts(foo);, and generates code accordingly (i.e., generates code to call puts instead of printf). Moving the format string to a global array, and passing that as the format string produces identical output, but forces it to be produced via printf instead of puts. Of course, it’s possible they might optimize around this some day as well, but at least for now (g++ 5.1) a test with g++ -O3 -S confirms that it’s actually calling printf (where the previous code compiled to a call to puts).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 284k
  • Answers 284k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer A more gentle introduction of Seams into the application could… May 13, 2026 at 4:25 pm
  • Editorial Team
    Editorial Team added an answer Apply a RotateTransform to the Image, with its initial Angle… May 13, 2026 at 4:25 pm
  • Editorial Team
    Editorial Team added an answer Short and simple: yes, you have to remember this for… May 13, 2026 at 4:25 pm

Related Questions

I've heard it said that the Entity Framework is overkill or that it's difficult
I've been learning the new ASP.NET MVC framwork lately and I've developed a test
I have a small database that I need help designing. I have a VB.NET
I'm planning to develop my own simple and elegant web application framework in C#
EDIT Thanks for the prompt responses. Please see what the real question is. I

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.