MikeMirzayanov's blog

By MikeMirzayanov, 15 years ago, translation, In English
If you write solutions on C++ it regularly happens than input reading through std::cin appears to be slow because of the large input size. Certainly is more correct in such cases to write data reading more effectively - at least using scanf. But if the testing system uses GNU C++ (checked on MinGW 4.4.1, but I think it works on other versions too), and you don't want to rewrite input reading, it is possible to improve performance by only one line placed in the beginning of the program: ios_base::sync_with_stdio(0).

On my example where it was required to find the sum of one million integers, it has accelerated the program in 4.5 times. Tried to do the same test on MS Visual C ++ 9.0 - but it hasn't accelerated the reading.

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

| Write comment?
13 years ago, # |
  Vote: I like it 0 Vote: I do not like it
tooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooold
  • 11 years ago, # ^ |
      Vote: I like it -6 Vote: I do not like it

    some one doesn't say anything twice unless he has a good reason for it ;)

»
11 years ago, # |
  Vote: I like it +11 Vote: I do not like it

In my case , it is not working . See 4082110, it has been accepted WITH OUT using "ios_base::sync_with_stdio(0)" , but when i used it in 4082115 , i got TLE .

What is wrong here ?

  • »
    »
    11 years ago, # ^ |
      Vote: I like it +5 Vote: I do not like it

    Now your code WITH "ios_base::sync_with_studio(0)" gets Accepted 4083080.

    • »
      »
      »
      11 years ago, # ^ |
        Vote: I like it +16 Vote: I do not like it

      What's the difference between these two code? I haven't find any change. :(

      • »
        »
        »
        »
        11 years ago, # ^ |
        Rev. 2   Vote: I like it -13 Vote: I do not like it

        One of them has the line "ios_base::sync_with_studio(0)", but other doesn't.

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

        when U use cin cout & scanf printf together the c++ itself syncs theme with each other it means that if U cout 1 then printf 2 then cout 3 then printf 4 it handles the problem of different buffers & you'll face this output:1 2 3 4 but if U place that code in the beginning of your code U'll surely face 1 before 3 & 2 before 4 but U can face 1 3 2 4 ,1 2 3 4, 2 4 1 3 ... but this disables a check which slows the code so U shouldn't use scanf & printf when this check is disabled but disabling the check isn't some times effective enough & you should use scanf printf to make a code quicker but if U want to make the code the quickest U can read the output by getchar() it's the quickest way but having it's own problems which isn't sometimes a good idea.anyway it's on your own decision to choose which one.

»
7 years ago, # |
  Vote: I like it -34 Vote: I do not like it

But sometimes using scanf is not the fastest when reading too large input size.

We can use getchar() to read integer byte by byte to make the input much faster and avoid TLE.