No more TLEs after this video !!! || Amazing Fix || endl VS "\n" in C++

Поділитися
Вставка
  • Опубліковано 8 лют 2021
  • This is one of the mistakes that every beginner in Competitive Programming makes. I personally had a hard time learning about this and correcting my bad habit. I thought I should definitely tell you guys about this so that you don't waste a lot of time figuring it out yourself.
    Like, Share and Subscribe in case you like the video and the efforts that I put into making these videos.

КОМЕНТАРІ • 57

  • @PriyanshAgarwal
    @PriyanshAgarwal  3 роки тому +16

    If you have watched this video completely and understood the reason why endl is slow, I am pretty sure you will use endl in interactive problems from now on (if you weren't already using it) as you don't have to flush the output manually every time, endl does that for you automatically. Happy Coding

  • @apurvpatani
    @apurvpatani 3 роки тому +47

    Btw if you're in the habit of using endl, you can simple add a #define endl '
    ' and you're good to go

    • @PriyanshAgarwal
      @PriyanshAgarwal  3 роки тому +20

      Thanks for adding this. I forgot to mention it❤️

    • @parthchhabra9427
      @parthchhabra9427 3 роки тому +8

      Just don't forget to remove it in an interactive problem.

    • @PriyanshAgarwal
      @PriyanshAgarwal  3 роки тому +6

      @@parthchhabra9427 ohh right. endl flushes every line automatically so I dont use '
      ' in interactive problems endl is sufficient in that.
      Thanks for adding this too❤️.

    • @Amritanjali
      @Amritanjali 3 роки тому

      @@PriyanshAgarwal what if we dont flush and use
      in iterative as well

    • @PriyanshAgarwal
      @PriyanshAgarwal  3 роки тому +3

      @@Amritanjali You will get an Idleness Limit Exceeded Error as the interactor won't know you are trying to print something unless you actually flush it.

  • @daddyofalltrades
    @daddyofalltrades 2 роки тому +9

    So basically,
    If you need faster code, use
    .
    If you need to work with less RAM, at the cost of speed, use endl.

  • @yashpriyadeepkatta8823
    @yashpriyadeepkatta8823 3 роки тому +6

    Brother, just wondering how many hours per day did you put in during your initial days( first 2 months ... Maybe) of CP?

  • @UCSGudduKumar
    @UCSGudduKumar 3 роки тому +5

    Yesterday's codechef Div 3 made this video a lot more important 😂

  • @yogeshyogendra648
    @yogeshyogendra648 3 роки тому +5

    Ye toh mujhe bhi nahi pata tha... thanks for the information ❤️

  • @sumekagarwal8229
    @sumekagarwal8229 3 роки тому

    Thank you bhiaya for making us hear this craziest news.

  • @sanjayreddy2949
    @sanjayreddy2949 9 місяців тому +1

    I use python for dsa , do i get more time exceeded errors while running the code compared to cpp or java

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

    is it required or recommended to use "
    " for the people who doesn't do competitive program?

  • @kirtanprajapati8464
    @kirtanprajapati8464 3 роки тому

    Thankyou so much bro for sharing this information bcs I don't know about it.
    THX😍

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

    this video gave rise to TLE Eliminators :)

  • @AnuragKumar-cb5lr
    @AnuragKumar-cb5lr 3 роки тому +4

    Was aware of that
    works faster than endl but not exepected that there is a huge diff between time limit..

    • @PriyanshAgarwal
      @PriyanshAgarwal  3 роки тому +8

      Yes, there is a very huge difference indeed. I am not sure if you've tried using endl on CodeChef, they have these insane strict time limits and the code always TLEs even though the algorithm is correct and optimal. You should also use fastio while doing CP. I think I will make a video on fastio some day when I am free.

    • @AnuragKumar-cb5lr
      @AnuragKumar-cb5lr 3 роки тому

      @@PriyanshAgarwal Actually it happened with me at Codechef, where precomputation was giving TLE, I endup skiping the question.

  • @priyanshubari9685
    @priyanshubari9685 Місяць тому +1

    very helpful thanks!

  • @onkarbirajdar5398
    @onkarbirajdar5398 3 роки тому

    Thank alot man👍

  • @parthchhabra9427
    @parthchhabra9427 3 роки тому

    Very useful!

  • @anshshah4927
    @anshshah4927 3 роки тому +3

    Bro can you help me develop logic?im only able to solve div2 a and div3 a,b,c

    • @PriyanshAgarwal
      @PriyanshAgarwal  3 роки тому +5

      Try practising problems of your difficulty X + 200-300 if your rating is X. Try practising more constructive algorithms based problems for developing better logic.

  • @techmelon7
    @techmelon7 6 місяців тому

    But "
    " uses 1 byte memory
    So, if we are printing it for 10^6 times, then it will consume 10^6 bytes (1.0 MB) more than the case in which we are using endl
    So the trade is between memory and time, right?
    Please correct me if I'm wrong...

  • @achintyaeeshan3069
    @achintyaeeshan3069 3 роки тому +2

    Nice video, though in interactive problems one might want to go with endl.

    • @PriyanshAgarwal
      @PriyanshAgarwal  3 роки тому

      Totally agree. I myself use endl for interactive problems

    • @tusharsaxena8239
      @tusharsaxena8239 3 роки тому

      Actually u have to go with endl or flush else you will get idleness error

    • @youknowwho9017
      @youknowwho9017 3 роки тому +2

      What is an interactve problem bro?

  • @nskybytskyi
    @nskybytskyi 3 роки тому +2

    just turn off the sync between std{in,out} and std::c{in,out} with
    ios::sync_with_stdio(false),cin.tie(nullptr),cout.tie(nullptr);
    and std::endl will not flush anything
    (the last one is technically not necessary, but I like it safe)

    • @PriyanshAgarwal
      @PriyanshAgarwal  3 роки тому +4

      You are talking about fast input/output here. But even if you do this, using "
      " instead of endl will still reduce the runtime. These runtimes that I have shown are after I have already done what you have suggested. But yeah thanks for suggesting this, fastio is a really cool thing. And yeah your channel is too good, I often watch those atcoder editorial videos on your channel xD.

    • @nskybytskyi
      @nskybytskyi 3 роки тому

      @@PriyanshAgarwal hmmm, I just tested it on some random problem (last four submissions in codeforces.com/submissions/Skybytskyi.Nikita), and endl indeed takes much more (2573ms) than "
      " (93ms) for 1 million numbers, but you very rarely need to print a million numbers, so I tested on 100 000 instead, and endl only takes 249ms, so it should not be the reason of many TLEs

    • @PriyanshAgarwal
      @PriyanshAgarwal  3 роки тому +2

      ​@@nskybytskyi Yeah but just imagine a question of Square root decomposition and printing 10^5 lines will definitely matter if the time limits are strict. But Yeah I get your point, it is not totally essential for many problems

  • @justlc7
    @justlc7 3 місяці тому

    Where can I find the code?

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

    @Priyansh Agarwal
    Makes understandable videos on codeforces blog 😎

  • @Surya045_
    @Surya045_ 2 роки тому

    I'm here after reading ur blog ,this video is helpful
    but don't know why that blog has more dislikes

    • @PriyanshAgarwal
      @PriyanshAgarwal  2 роки тому +1

      Because I gave it a clickbait title initially 😂

    • @Surya045_
      @Surya045_ 2 роки тому

      @@PriyanshAgarwal ohh 😅

  • @subhankarmanna3770
    @subhankarmanna3770 3 роки тому

    Bhaiya java ka istarha koi tips bataiye na...!!!!

  • @lfcpratiksharma105
    @lfcpratiksharma105 3 роки тому

    Merko pata tha ki
    taz hota hei par bhai ji itna taz sir dard hi ho gai ye sun ke to .🤣🤣

  • @yourbestie4138
    @yourbestie4138 3 роки тому +1

    Thanks

  • @srishtikumari5230
    @srishtikumari5230 3 роки тому

    Could you please give a solution for MLE also??

    • @PriyanshAgarwal
      @PriyanshAgarwal  3 роки тому

      Some quick checks:
      1) Stack overflow in recursion
      2) Infinite loop in which you are declaring some array or using any memory for that matter.
      3) Sometimes just a TLE can give you MLE because you are also using the memory at the same time.
      4) Keep the array size below 1e7 for 256 MB memory limit. If you are declaring a DP make sure you are not making a matrix that has more than 1e7 elements.

    • @srishtikumari5230
      @srishtikumari5230 3 роки тому

      @@PriyanshAgarwal Thanks

  • @zaico09
    @zaico09 3 роки тому

    What about cin/cout and scanf/printf?

    • @PriyanshAgarwal
      @PriyanshAgarwal  3 роки тому +3

      I am not sure but most people still use cin, cout instead of scanf, printf. But I agree using scanf, printf makes it even faster xD

  • @girishgarg2816
    @girishgarg2816 3 роки тому

    It would be better if you add link to any problem which gives tle on using endl and not on
    . Personally, I have not yet found a problem where I got tle because of this

    • @girishgarg2816
      @girishgarg2816 3 роки тому

      This is good for saving time while working on projects. But I don't think using endl in cp will cause you tle

    • @PriyanshAgarwal
      @PriyanshAgarwal  3 роки тому +1

      You can look at my 2 submissions for the same problem and see the time difference in them.
      codeforces.com/contest/959/submission/107401507 561ms with endl
      codeforces.com/contest/959/submission/107400431 61ms with "
      ".
      Here there were only 10 ^ 5 lines. Now imagine the time difference when there are 10 ^ 6 lines.

  • @saiprashanth3597
    @saiprashanth3597 3 роки тому

    :orz: