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 8404507
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T22:37:02+00:00 2026-06-09T22:37:02+00:00

#include <iostream> #include <cstdlib> #include <cstdio> #include <ctime> int main(int argc, char* argv[]) {

  • 0
#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <ctime>

int main(int argc, char* argv[])
{
    std::clock_t start;
    double duration;    

    std::cout << "Starting std::cout test." << std::endl;
    start = std::clock();

    for (int i = 0; i < 1000; i++)
    {
        std::cout << "Hello, World! (" << i << ")" << std::endl;
    }

    duration = (std::clock() - start) / (double) CLOCKS_PER_SEC;

    std::cout << "Ending std::cout test." << std::endl;
    std::cout << "Time taken: " << duration << std::endl;

    std::system("pause");

    std::cout << "Starting std::printf test." << std::endl;
    start = std::clock();

    for (int i = 0; i < 1000; i++)
    {
        std::printf("Hello, World! (%i)\n", i);
        std::fflush(stdout);
    }

    duration = (std::clock() - start) / (double) CLOCKS_PER_SEC;

    std::cout << "Ending std::printf test." << std::endl;
    std::cout << "Time taken: " << duration << std::endl;

    system("pause");

    return 0;
}

Now, here are the times for the first five runs:

  • std::cout test: 1.125 s ; printf test: 0.195 s
  • std::cout test: 1.154 s ; printf test: 0.230 s
  • std::cout test: 1.142 s ; printf test: 0.216 s
  • std::cout test: 1.322 s ; printf test: 0.221 s
  • std::cout test: 1.108 s ; printf test: 0.232 s

As you can see, using printf and then fflushing takes about 5 times less time than using std::cout.

Although I did expect using std::cout‘s << operator to be perhaps a little slower (almost minimal) , I wasn’t prepared for this huge difference. Am I making a fair test? If so, then what makes the first test so much slower than the second one, if they essentially do the exact same thing?

  • 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-06-09T22:37:04+00:00Added an answer on June 9, 2026 at 10:37 pm

    For a true apples-to-apples comparison, re-write your test so that the only thing changing between the test cases is the print function being used:

    int main(int argc, char* argv[])
    {
        const char* teststring = "Test output string\n";
        std::clock_t start;
        double duration;
    
        std::cout << "Starting std::cout test." << std::endl;
        start = std::clock();
    
        for (int i = 0; i < 1000; i++)
            std::cout << teststring;
        /* Display timing results, code trimmed for brevity */
    
        for (int i = 0; i < 1000; i++) {
            std::printf(teststring);
            std::fflush(stdout);
        }
        /* Display timing results, code trimmed for brevity */
        return 0;
    }
    

    With that, you will be testing nothing but the differences between the printf and cout function calls. You won’t incur any differences due to multiple << calls, etc. If you try this, I suspect that you’ll get a much different result.

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

Sidebar

Related Questions

This program: #include <iostream> #include <cstdlib> #include <string> int main(int argc, const char *argv[])
#include <cstdlib> #include <iostream> int main(int argc, char *argv[]) { cout << "size of
#include<iostream> #include<fstream> #include<cstdlib> #include<string> using namespace std; **int main() { double write(); double read();
#include<iostream> #include<fstream> #include<cstdlib> #include<iomanip> using namespace std; int main() { ifstream in_stream; // reads
Take the following program: #include <cstdlib> using std::rand; #include <iostream> using std::cout; int main()
#include <iostream> #include <fstream> #include <cstdlib> using namespace std; const int FILENAME_MAX=20; int main()
#include <iostream> #include <fstream> #include <cstdlib> int main() { std::fstream f1(/tmp/test); if (!f1) {
This is a small program: #include <iostream> #include <cstdlib> using namespace std; int main()
i have following code #include <iostream> #include <cstdlib> using namespace std; int main() {
#include <iostream> #include <fstream> #include <cstdlib> using namespace std; int main() { fstream inOutGrades(

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.