Fast IO in C++ for Competitive Programming! (and when to not use it)

preview_player
Показать описание
Fast IO makes your code faster, but has some tradeoffs. Learn how it is faster, when to use it, and when to avoid it.

Correction at 1:19, it removes the sync between all C and C++ standard streams, not just printf and cout.

Shoutout to my friends for reviewing this video! :D

======

Hello! I am sriv (AKA crap_the_coder). I will be making videos on competitive programming on topics that are tricky to understand, or topics that aren't discussed much. I am planning to cover topics of all difficulties. If you are a beginner or an intermediate programmer, you'll probably find my videos helpful. :D

======

======
Рекомендации по теме
Комментарии
Автор

Excellent video!

Just to clarify some stuff -

*For those still wondering why using cin and cout is slower -*
cin is by default tied to cout. This means that whenever you input (cin) something, your program checks whether your output buffer is empty or not.
If not, it prints or "flushes" the buffer. This check or flushing is slow can sometimes cause your program to TLE.

*How to make cin and cout faster?*
By using fastio. cin.tie(nullptr) breaks the tie between cin and cout and so your program doesn't check the buffer when you input.
This makes your code faster.

*However, be careful while using fastio in interactive problems*
In interative problems, you don't get all the input before the outputs. Because of this, you need to flush your buffer.
There are two main ways of solving this issue.
1) Don't use fastio for interactive problems.
2) Use fastio but flush your outputs with the help of endl.
endl does two things. It prints a newline like '\n' does. Secondly, it also flushes the output.
*NOTE: **_Please make sure not to use '\n' with fastio if you want to flush your output._*

I hope that made things clearer.
Cheers!

evenvalue
Автор

Now all beginners in CP will know the actual meaning of these 2 lines. Very well explained sir.

shivanksharma
Автор

Just a clarification by the way. You can in fact use fast IO with interactive problems, but you strictly have to use endl instead of '\n' in that case.
But again, this negates the effect of using cin.tie(NULL);

So for interactive problems, you should either remove fast IO (which I think is safer), or you should use endl instead of '\n'.

sriv
Автор

you will become popular sir
just keep doing videos like this !!
gem for cp'ers

codecorn
Автор

Your explanation is so crisp and clear! 🥰

khyatichaudhary
Автор

Now I know the reason of huge fan following of Sriv

kshitizjaiswal
Автор

Excellent video!! Cleared so may doubts..

howdadogdoin
Автор

hey thank you for starting this
gonna start with you!

_shivendsingh
Автор

Awesome video, it was really helpful! Just a clarification, in case of non-interactive problems (the normal ones), using "\n" is better than endl because in case we have multiple couts between two cins, the couts are flushed just once as the second cin is called when using "\n", which is not the case in endl: it is flushed as many times as couts are called. This makes using "/n" slightly faster due to less number of flushes. Is this correct?

AarshWankarIITGN
Автор

I did fast IO in the beginning of cp, but now I am targeting an audience of people with my code.
Today I would like people to read my code and be able to understand it as fast as possible.

Now I try to optimize human brain fastIO instead of computer fastIO 😀

egor.okhterov
Автор

hay bro whenever i try to read a codeforces problem i am not able to understand what they are telling what should i do

GM-pkli
Автор

Hi, I just saw your video on Priyansh sir's youtube channel. This year I will go to Class 11th and I am learning DSA right now. Regarding ZCO and all, how do i apply actually? I would be glad if you can guide me 😊

shiv.shankar
Автор

although i used but still cout and printf giving output in order

Yashkumar-fnjw
Автор

hey are you the same guy from TLE Eliminators ?

iam-Coder
Автор

sir what you mean by interactive problems?

ShashidharM-hv
Автор

can u please tell which mic do u use ?

isFraggyLive
Автор

Your explanation is great .. pls explain in little bit slow.

AshishKumar-pofn
join shbcf.ru