Movatterモバイル変換


[0]ホーム

URL:



Codeforces
In EnglishПо-русски
Enter |Register



→ Pay attention
Before contest
Codeforces Round 1037 (Div. 3)
4 days
→ Streams
Before stream04:24:18
View all →
→ Top rated
#UserRating
1jiangly3756
2tourist3723
3orzdevinwang3696
4Kevin1145143647
5Radewoosh3631
6ecnerwala3596
7Benq3527
8maroonrk3518
9ksun483484
10Nachia3463
Countries |Cities |OrganizationsView all →
→ Top contributors
#UserContrib.
1errorgorn169
2Qingyu165
3Dominater069159
4cry157
4Um_nik157
6adamant155
7-is-this-fft-152
8djm03178148
9soullless146
10chromate00144
View all →
→ Find user
→ Recent actions
Detailed →

ed1d1a8d's blog

By ed1d1a8d,13 years ago,In English

What form of I/O should we use in competitive programming?

For examplescanf andprintf are undoubtedly faster thancin andcout, but the later two are easier and faster to code. How should we adress this tradeoff?

  • Vote: I like it
  • 0
  • Vote: I do not like it

»
13 years ago,show (+1)#
»
13 years ago,hide#|
 
Vote: I like it+1Vote: I do not like it

Just includestd::ios::sync_with_stdio(false); in your code, and cin will be as fast as scanf.

»
13 years ago,show (+1)#
»
13 years ago,hide#|
 
Vote: I like it+12Vote: I do not like it

Likemukel said,ios_base::sync_with_stdio(0) makes c++ io streams' performance comparable to cstdio's. Have a look at the following thread:http://codeforces.com/blog/entry/925, as well as here:The Standard Librarian: IOStreams and Stdio.

Once, at another Online Judge, I kept getting TLE's for an algorithm that was quite IO intensive. I was using cin/cout andios_base::sync_with_stdio(0) as the first line of main()'s body. I switched back to scanf/printf and got AC. Depends on the implementation. To me, it works wonderfully at CodeForces.

PD:Also, favor '\n' instead of endl for ending lines. endl flushes the buffer every time.

  • »
    »
    13 years ago,show (+1)#
    »
    »
    13 years ago,hide#^|
     
    Vote: I like it0Vote: I do not like it

    Does this apply to all streams? i.e. reading files with ifstream or ofstream

    • »
      »
      »
      13 years ago,show (+1)#
      »
      »
      »
      13 years ago,hide#^|
       
      Vote: I like it+1Vote: I do not like it

      ios_base::sync_with_stdio() only affects standard streams; there's probably nothing you can do to speed upifstream/ofstream, except for not usingendl or changing their buffer sizes.

      • »
        »
        »
        »
        11 years ago,show (+1)#
        »
        »
        »
        »
        11 years ago,hide#^|
         
        Vote: I like it0Vote: I do not like it

        But fstream is even faster than iostream with ios_base::sync_with_stdio()

        • »
          »
          »
          »
          »
          11 years ago,show#
          »
          »
          »
          »
          »
          11 years ago,hide#^|
           
          Vote: I like it0Vote: I do not like it

          This is placebo. The function only affects standard streams. In case offstream there is not even a corresponding C stream to synchronize with.

    »
    13 years ago,hide#|
     
    Vote: I like it+5Vote: I do not like it

    but also notice: withstd::ios::sync_with_stdio(false);, don't mix C-style IO (scanf/printf, gets/puts, getchar/putchar ... ) with C++-style IO (cin/cout ... ), since cin/cout is not sync with stdio, you may get same data from using scanf and then use cin again.

      »
      13 years ago,show#
      »
      13 years ago,hide#|
       
      Vote: I like it0Vote: I do not like it

      You might find this post of mine useful:http://codeforces.com/blog/entry/5217

        »
        13 years ago,show#
        »
        13 years ago,hide#|
         
        Vote: I like it0Vote: I do not like it
          »
          11 years ago,show (+1)#
          »
          11 years ago,hide#|
          Rev.2  
          Vote: I like it0Vote: I do not like it

          Put this:ios_base::sync_with_stdio(false);cin.tie(0); in the beggining of the main func. cin/cout will be as fast as scanf and printf (even faster). BUT DONT USE ENDL (use "\n")

          »
          11 years ago,show#
          »
          11 years ago,hide#|
           
          Vote: I like it0Vote: I do not like it

          I use stringstream to store output first and output it on the last line ascout << ss.str();.

          This works great when you input and output alternatively. (For example, when you output as you answer query)

            »
            10 years ago,show (+1)#
            »
            10 years ago,hide#|
             
            Vote: I like it0Vote: I do not like it

            fastest I is getchar_unlocked()

            • »
              »
              10 years ago,show (+1)#
              »
              »
              10 years ago,hide#^|
               
              Vote: I like it+19Vote: I do not like it

              just found out this blog is 2 years old ;|

              • »
                »
                »
                10 years ago,show (+1)#
                »
                »
                »
                10 years ago,hide#^|
                 
                Vote: I like it+10Vote: I do not like it

                Two years old, but no one else said anything about getchar_unlocked =P.

                It was really hard to convince myself to move from C to C++ half a decade ago, but I did. I never liked cin/cout though, so I still use scanf/printf instead, and that probably won't change.

                In the very rare times I need fast input reading (like when the input size is O(n) and an O(n) solution is expected, but mine is O(n lg n)):for (x = 0; (c = getchar_unlocked()) >= '0'; x = x*10+c-'0'); // reads int to x

                Usually I write it as the following function:

                inline void scan_uint(int* p) {    static char c;    while ((c = getchar_unlocked()) < '0'); // just to be safe    for (*p = c-'0'; (c = getchar_unlocked()) >= '0'; *p = *p*10+c-'0');}

                Making it parse input as float, string, int with sign, return if eof, etc should be easy too... getchar_unlocked already gave me two or three balloons, so I'm grateful to it =)!

            »
            10 years ago,hide#|
            Rev.3  
            Vote: I like it+14Vote: I do not like it

            I use this recently, and work very good :


            using namespace std;typedef long long i64;const int MAX_STRING_LENGTH = 1;#define endl "\n"struct _io{ char buff[MAX_STRING_LENGTH]; _io & operator >>(int &x){ scanf("%d",&x); return *this; } _io & operator >>(i64 &x){ scanf("%I64d",&x); return *this; } _io & operator >>(float &x){ scanf("%f",&x); return *this; } _io & operator >>(double &x){ scanf("%lf",&x); return *this; } _io & operator >>(long double &x){ scanf("%llf",&x); return *this; } _io & operator >>(char &c){ scanf("%c",&c); return *this; } _io & operator >>( char *s ){ scanf("%s",s); return *this; } _io & operator >>( string &s ){ scanf("%s",buff); s = string(buff,strlen(buff)); return *this; } _io & operator <<(int x){ printf("%d",x); return *this; } _io & operator <<(i64 x){ printf("%I64d",x); return *this; } _io & operator <<(float x){ printf("%f",x); return *this; } _io & operator <<(double &x){ printf("%lf",x); return *this; } _io & operator <<(long double &x){ printf("%llf",x); return *this; } _io operator <<(char &c){ printf("%c",c); return *this; } _io & operator <<( string &s ){ printf("%s",s.c_str()); return *this; } _io & operator <<( char *s ){ printf("%s",s); return *this; }}io;int main(){int a, b; io>>a>>b;io<<(a+b)<<endl;}
            »
            10 years ago,show (+1)#
            »
            10 years ago,hide#|
             
            Vote: I like it0Vote: I do not like it

            Hey guys, thanks for such an excellent discussion. I've read the entire page, but forgive me if I missed something, as I am going to ask a stupid question. * What if sometimes I want to usecincout and sometimesprintf,scanf etc. in the same code ? I mean, can I switchON / OFFstd::ios::sync_with_stdio(); whenever I want ?

            • »
              »
              10 years ago,show#
              »
              »
              10 years ago,hide#^|
               
              Vote: I like it+3Vote: I do not like it

              You wouldn't want to switch off thesync and then useboth cin/cout and scanf/printf as it "may result in unexpectedly interleaved characters".

              As it has been thoroughly explained above, cin/cout with syncing off and '\n' instead of endl should be faster than scanf / printf because the latter should undergo parsing all the time.

              You must call the function before any input / output is done, so no, you shouldn't be toggling it.

              »
              9 years ago,show#
              »
              9 years ago,hide#|
               
              Vote: I like it+5Vote: I do not like it

              I don't like tradeoffs, so I ended up writing my own versionhttp://codeforces.com/blog/entry/45835

                »
                7 years ago,show (+1)#
                »
                7 years ago,hide#|
                 
                Vote: I like it0Vote: I do not like it

                Before few minutes, I have solved Light oj problem no:1087,problem title:"Diablo". In that problem, at first, I have usedcin/count along withios_base::sync_with_stdio(false);cin.tie(0);. I have also used#define endl "\n" and used noprintf/scanf, which gave meRuntime Error. Later on, I have given up only this partcin.tie(0); and everything was similar like the before which gave meTime Limit Exceed. At last, I have used onlyprintf/scanf which gave meAccepted.

                Can any body explain it? (Thanks In Advance)

                • »
                  »
                  5 years ago,show (+1)#
                  »
                  »
                  5 years ago,hide#^|
                   
                  Vote: I like it0Vote: I do not like it

                  I also had same experience for light oj problem no. 1088. Please explain if anybody knows why?? is it platform issue or something else??

                  • »
                    »
                    »
                    5 years ago,show#
                    »
                    »
                    »
                    5 years ago,hide#^|
                     
                    Vote: I like it0Vote: I do not like it

                    LightOJ has some weird issues with fast IO. Usescanf/printf in LightOJ and you should be fine.


                   
                   
                  In EnglishIn Russian



                  Codeforces (c) Copyright 2010-2025 Mike Mirzayanov
                  The only programming contests Web 2.0 platform
                  Server time:Jul/13/2025 07:35:42 (k3).
                  Desktop version, switch tomobile version.
                  Privacy Policy |Terms and Conditions
                  Supported by
                  TON
                   
                  ITMO University
                   
                   
                   
                   
                  User lists
                   
                   
                  Name

                  [8]ページ先頭

                  ©2009-2025 Movatter.jp