2

My mentors in CP recommended me to use ios_base::sync_with_stdio(0); since it increases the speed of program execution. While going through some videos on YouTube, I came across ios::sync_with_stdio(0); also.

So, What difference does adding or deleting _base make?

Which is better, ios_base::sync_with_stdio(0); or ios::sync_with_stdio(0);?

Kindly explain. Thanking you in advance.

9
  • Step through them with a debugger to find out their difference. Commented Jan 8, 2021 at 8:59
  • 1
    Don't use either unless you have a good reason to. Do you understand where this speed increase comes from and do you really need it? Same question to your mentor. Commented Jan 8, 2021 at 9:00
  • 1
    None is better. ios::sync_with_stdio is a static method inherited from ios_base and it is literally the same thing. Commented Jan 8, 2021 at 9:03
  • 1
    @Evg Thank you for responding, sir. I've some inkling regarding that. Using this, ios_base::sync_with_stdio(0); synchronization between C and C++ standard streams is disabled. So, C++ has its own buffer independent from C which makes things work faster. My mentors recommended this since I was repeatedly getting Time Limit Exceeding while participating in Codechef's Long Challenge. Commented Jan 8, 2021 at 9:08
  • @freakish May you please explain what is static method ? Commented Jan 8, 2021 at 9:13

1 Answer 1

5

What is the difference between ios_base::sync_with_stdio(0); and ios::sync_with_stdio(0); in C++?

One takes 5 characters more _base to type. There are no other differences.

The function is defined as a static public member function in ios_base class. ios is really typedef basic_ios<char> ios; and basic_ios inherits from ios_base. As so, ios_base::sync_with_stdio is inherited from ios_base to basic_ios<char> and to ios. It's the same function. The same way you can std::wios::sync_with_stdio or std::basic_ios<wchar_t>::sync_with_stdio etc.

For more information see cppreference io, cppreference static members, cppreference sync_with_stdio, cppreference derived classes and I always propose to read a good C++ introduction book.

Which is better, ios_base::sync_with_stdio(0); or ios::sync_with_stdio(0);?

They are equal.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you! I'm not quite familiar with these classes and functions you mentioned but got the crux of your answer. Probably, in further studies as well, this detailed answer will help me understand the internal mechanisms of programming better. Thanks again!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.