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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T16:06:53+00:00 2026-05-31T16:06:53+00:00

I am coding for various programming olympiads and am trying to improve time efficiency.

  • 0

I am coding for various programming olympiads and am trying to improve time efficiency. I’m looking for the fastest way to get input, using the gcc compiler without any external library.

I’ve previously used cin and cout, but found that scanf and printf are much faster. Is there an even faster way? I don’t care of space complexity that much, I rather prefer better time.

  • 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-31T16:06:55+00:00Added an answer on May 31, 2026 at 4:06 pm

    That streams are always slower than the C-API functions is a pretty common misconception because by default, they synchronize with the C-layer. So yeah, that’s a feature, not a bug.

    Without sacrificing type safety (and readability, depending on your taste), you possibly gain performance with streams by using:

    std::ios_base::sync_with_stdio (false);
    

    A little indicator:

    #include <cstdio>
    #include <iostream>
    
    template <typename Test> 
    void test (Test t)
    {
        const clock_t begin = clock();
        t();
        const clock_t end = clock();
        std::cout << (end-begin)/double(CLOCKS_PER_SEC) << " sec\n";
    }
    
    void std_io() {
        std::string line;
        unsigned dependency_var = 0;
        
        while (!feof (stdin)) {
            int c;
            line.clear();
            while (EOF != (c = fgetc(stdin)) && c!='\n')
                line.push_back (c);
            dependency_var += line.size();
        }
        
        std::cout << dependency_var << '\n';
    }
    
    void synced() {
        std::ios_base::sync_with_stdio (true);
        std::string line;
        unsigned dependency_var = 0;
        while (getline (std::cin, line)) {
            dependency_var += line.size();
        }
        std::cout << dependency_var << '\n';
    }
    
    void unsynced() {
        std::ios_base::sync_with_stdio (false);
        std::string line;
        unsigned dependency_var = 0;
        while (getline (std::cin, line)) {
            dependency_var += line.size();
        }
        std::cout << dependency_var << '\n';
    }
    
    void usage() { std::cout << "one of (synced|unsynced|stdio), pls\n"; }
    
    int main (int argc, char *argv[]) {
        if (argc < 2) { usage(); return 1; }
        
        if (std::string(argv[1]) == "synced") test (synced);
        else if (std::string(argv[1]) == "unsynced") test (unsynced);
        else if (std::string(argv[1]) == "stdio") test (std_io);
        else { usage(); return 1; }
    
        return 0;
    }
    

    With g++ -O3, and a big text file:

    cat testfile | ./a.out stdio
    ...
    0.34 sec
    
    cat testfile | ./a.out synced
    ...
    1.31 sec
    
    cat testfile | ./a.out unsynced
    ...
    0.08 sec
    

    How this applies to your case depends. Modify this toy-benchmark, add more tests, and compare e.g. something like std::cin >> a >> b >> c with scanf ("%d %d %d", &a, &b, &c);. I guarantee, with optimizations (i.e. without being in debug mode), performance differences will be subtle.

    If that does not saturate your needs, you might try other approaches, e.g. reading the whole file first (may or may not bring more performance) or memory maps (which is a non-portable solution, but the big desktops have them).


    Update

    Formatted input: scanf vs. streams

    #include <cstdio>
    #include <iostream>
    
    template <typename Test> 
    void test (Test t)
    {
        const clock_t begin = clock();
        t();
        const clock_t end = clock();
        std::cout << (end-begin)/double(CLOCKS_PER_SEC) << " sec\n";
    }
    
    void scanf_() {
        char x,y,c;
        unsigned dependency_var = 0;
        
        while (!feof (stdin)) {
            scanf ("%c%c%c", &x, &y, &c);
            dependency_var += x + y + c;
        }
        
        std::cout << dependency_var << '\n';
    }
    
    void unsynced() {
        std::ios_base::sync_with_stdio (false);
        char x,y,c;
        unsigned dependency_var = 0;
        while (std::cin) {
            std::cin >> x >> y >> c;
            dependency_var += x + y + c; 
        }
        std::cout << dependency_var << '\n';
    }
    
    void usage() { std::cout << "one of (scanf|unsynced), pls\n"; }
    
    int main (int argc, char *argv[]) {
        if (argc < 2) { usage(); return 1; }
        
        if (std::string(argv[1]) == "scanf") test (scanf_);
        else if (std::string(argv[1]) == "unsynced") test (unsynced);
        else { usage(); return 1; }
    
        return 0;
    }
    

    Results:

    scanf: 0.63 sec
    unsynced stream: 0.41 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to do some IP agnostic coding and as suggested by various sources
Ok, so I'm trying to weigh up the pro's and con's of using various
The coding is done using VS2008 There are two divs in my page namely
Introduction : Hello Everyone, I have been looking for days for a way to
I have various user controls that get placed in a content control within my
I am looking into the various options around garbage collection on a Java 6
I am using the iphone SDK and coding primarily in C++ while using parts
I'm coding my first website using XML+XSL. The xml I am transforming have this
I have been coding for a while now but just can't seem to get
I have been coding in python/django and c/c++ from last three years for various

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.