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...
    ======

КОМЕНТАРІ • 48

  • @evenvalue
    @evenvalue Рік тому +30

    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!

  • @shivanksharma6880
    @shivanksharma6880 Рік тому +24

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

    • @sriv
      @sriv  Рік тому +2

      Thank you very much :D

  • @sriv
    @sriv  Рік тому +8

    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 '
    '.

  • @khyatichaudhary4083
    @khyatichaudhary4083 Рік тому +3

    Your explanation is so crisp and clear! 🥰

    • @sriv
      @sriv  Рік тому

      Thanks :DD

  • @khyatichaudhary4083
    @khyatichaudhary4083 Рік тому +7

    TLE has a gem!💯

  • @codecorn8030
    @codecorn8030 Рік тому +1

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

  • @egor.okhterov
    @egor.okhterov 4 місяці тому +1

    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 😀

  • @kshitizjaiswal6870
    @kshitizjaiswal6870 Рік тому +3

    Now I know the reason of huge fan following of Sriv

  • @Ashishkumar-oh8ww
    @Ashishkumar-oh8ww Рік тому +6

    Nori here !!! U are orz sir.

  • @037_shivendsingh5
    @037_shivendsingh5 Рік тому +3

    hey thank you for starting this
    gonna start with you!

    • @sriv
      @sriv  Рік тому

      Thanks! And good luck! :D

  • @manishkandpal1762
    @manishkandpal1762 Рік тому +1

    Great explanation 💯

  • @shiv.shankar
    @shiv.shankar Рік тому +2

    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 😊

    • @sriv
      @sriv  Рік тому +2

      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.

    • @shiv.shankar
      @shiv.shankar Рік тому +2

      @@sriv Thanks a lot

  • @mananraina
    @mananraina Рік тому +2

    Well explained

    • @sriv
      @sriv  Рік тому

      Thank you!

  • @harshvardhanpandey8057
    @harshvardhanpandey8057 Рік тому +1

    Nice explanation!

    • @sriv
      @sriv  Рік тому

      Thank you! :D

  • @vishala8788
    @vishala8788 Рік тому +1

    Amazing sir

  • @justvibing9663
    @justvibing9663 Рік тому +2

    Sriv orz🛐

  • @AshishKumar-po1fn
    @AshishKumar-po1fn Рік тому +1

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

    • @sriv
      @sriv  Рік тому

      Thank you! I will keep that in mind!

  • @Yashkumar-fn1jw
    @Yashkumar-fn1jw 11 місяців тому

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

  • @deepankarind
    @deepankarind Рік тому

    Please uploaded more videos 🙏

  • @iam-Coder
    @iam-Coder 2 місяці тому

    hey are you the same guy from TLE Eliminators ?

  • @ShashidharM-h9v
    @ShashidharM-h9v 8 місяців тому

    sir what you mean by interactive problems?

  • @GM-pk4li
    @GM-pk4li 9 місяців тому

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

  • @Thor_49
    @Thor_49 Рік тому +1

    🙌🙌

  • @anonymous10906
    @anonymous10906 8 місяців тому

    best!

  • @kamalchhimwal9362
    @kamalchhimwal9362 Рік тому +1

    OG🐱

  • @isFraggyLive
    @isFraggyLive Рік тому

    can u please tell which mic do u use ?

    • @sriv
      @sriv  Рік тому

      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

    • @isFraggyLive
      @isFraggyLive Рік тому

      @@sriv do u use audacity ..or which software for removing noise

    • @sriv
      @sriv  Рік тому

      @@isFraggyLive I use premiere pro

  • @red24_
    @red24_ Рік тому +1

    orz

  • @ananthramvijayaraj4035
    @ananthramvijayaraj4035 Рік тому +1

    hso sir

    • @sriv
      @sriv  Рік тому

      Nou sir

  • @akshay6332
    @akshay6332 Рік тому +2

    Sriv Orz🤌♥️

    • @sriv
      @sriv  Рік тому

      Nou sir