Fast IO in C++ for Competitive Programming! (and when to not use it)
Вставка
- Опубліковано 5 жов 2024
- 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
Join my discord server to discuss, ask doubts, or just hang out! / discord
======
General: sriv.bio.link/
LinkedIn: / srivaths-p
Twitter: / crapthecoder
GitHub: github.com/Cra...
CodeChef: codechef.com/u...
CodeForces: codeforces.com...
======
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 '
' does. Secondly, it also flushes the output.
*NOTE: **_Please make sure not to use '
' with fastio if you want to flush your output._*
I hope that made things clearer.
Cheers!
Thanks a lot for all the insight! :D
Naveen orz
Now all beginners in CP will know the actual meaning of these 2 lines. Very well explained sir.
Thank you very much :D
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 '
' 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 '
'.
Your explanation is so crisp and clear! 🥰
Thanks :DD
TLE has a gem!💯
you will become popular sir
just keep doing videos like this !!
gem for cp'ers
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 😀
Now I know the reason of huge fan following of Sriv
xD
Nori here !!! U are orz sir.
Hello sir! :D
@@sriv :)
hey thank you for starting this
gonna start with you!
Thanks! And good luck! :D
Great explanation 💯
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 😊
Hey! Here's a very important link which contains basically all the information about ICO: www.iarcs.org.in/inoi/
Applications are not open right now, and it likely won't be open for a few months.
Here is another link with all the details: www.codingninjas.com/codestudio/library/indian-computing-olympiad-registration
Do participate in ICO and good luck with your preparation! :D
You can also message me at sriv#5123 on Discord if you'd like.
@@sriv Thanks a lot
Well explained
Thank you!
Nice explanation!
Thank you! :D
Amazing sir
Sriv orz🛐
Your explanation is great .. pls explain in little bit slow.
Thank you! I will keep that in mind!
although i used but still cout and printf giving output in order
Please uploaded more videos 🙏
hey are you the same guy from TLE Eliminators ?
sir what you mean by interactive problems?
hay bro whenever i try to read a codeforces problem i am not able to understand what they are telling what should i do
🙌🙌
best!
OG🐱
can u please tell which mic do u use ?
I use this: www.amazon.in/Zebronics-Zeb-KLARITY-Gaming-MIC/dp/B085NNH52P
Do note though, I use noise cancelling at 80% from my editing software to make it way better
@@sriv do u use audacity ..or which software for removing noise
@@isFraggyLive I use premiere pro
orz
Nou
hso sir
Nou sir
Sriv Orz🤌♥️
Nou sir