What the function 'std::ios_base::sync_with_stdio()' does?

Why do some people use std::ios_base::sync_with_stdio(false) at the beginning of their programs?
What do this function do? Why when I remove the function I don’t see any difference? Are there some benefit of using the function?

1 Like

http://en.cppreference.com/w/cpp/io/ios_base/sync_with_stdio

This should help.

Happy Coding.

It makes input and output faster.(cin and cout)

This disables the synchronization between the C and C++ standard streams.

You could also have a look at this.

http://codeforces.com/blog/entry/5217

1 Like

nice link:)

1 Like

From where do you came to know about sync_with_stdio first?

I was just looking at others code and and so I was curious and I looked it up online and found about it.

Can’t we rather include “cstdio” and use scanf & printf instead of using this sync function.

@coder_voder Using cin and cout with std::ios_base::sync_with_stdio()
makes it even faster than scanf and printf.

You can always use scanf and printf by including cstdio or stdio.h but sometimes (rarely) you need even faster i/o methods to pass the test cases. That’s when that sync function comes to rescue.

But scanf/printf still do a nice job. :slight_smile:

The two calls have different meanings that have nothing to do with performance; the fact that it speeds up the execution time is (or might be) just a side effect. You should understand what each of them does and not blindly include them in every program because they look like an optimization.

ios_base::sync_with_stdio(false);
This disables the synchronization between the C and C++ standard streams. By default, all standard streams are synchronized, which in practice allows you to mix C- and C+±style I/O and get sensible and expected results. If you disable the synchronization, then C++ streams are allowed to have their own independent buffers, which makes mixing C- and C+±style I/O an adventure.

Also keep in mind that synchronized C++ streams are thread-safe (output from different threads may interleave, but you get no data races).

cin.tie(NULL);
This unties cin from cout. Tied streams ensure that one stream is flushed automatically before each I/O operation on the other stream.

By default cin is tied to cout to ensure a sensible user interaction. For example:

std::cout << “Enter name:”;
std::cin >> name;
If cin and cout are tied, you can expect the output to be flushed (i.e., visible on the console) before the program prompts input from the user. If you untie the streams, the program might block waiting for the user to enter their name but the “Enter name” message is not yet visible (because cout is buffered by default, output is flushed/displayed on the console only on demand or when the buffer is full).

So if you untie cin from cout, you must make sure to flush cout manually every time you want to display something before expecting input on cin.

In conclusion, know what each of them does, understand the consequences, and then decide if you really want or need the possible side effect of speed improvement.

1 Like

Long story short:
This statement disables the synchronization in the c++ input functions thereby making them faster.
This is often used in competitive programming platforms for fast I/O.

When speed is the concern we use std::ios_base::sync_with_stdio(false) for faster I/O as compared to iostream in C++. However something even faster is getchar_unlocked().We can use getchar_unlocked for problem like: https://uva.onlinejudge.org/external/124/p12440.pdf
It is mentioned here Dataset is huge, use faster IO methods.
Use it when thread safety is not required but speed is.

P.S. You can read more about getchar_unocked() here - https://www.quora.com/How-and-when-do-I-use-getchar_unlocked-in-C

1 Like