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.
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
Btw if you're in the habit of using endl, you can simple add a #define endl '
' and you're good to go
Thanks for adding this. I forgot to mention it❤️
Just don't forget to remove it in an interactive problem.
@@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❤️.
@@PriyanshAgarwal what if we dont flush and use
in iterative as well
@@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.
So basically,
If you need faster code, use
.
If you need to work with less RAM, at the cost of speed, use endl.
Brother, just wondering how many hours per day did you put in during your initial days( first 2 months ... Maybe) of CP?
Yesterday's codechef Div 3 made this video a lot more important 😂
Ye toh mujhe bhi nahi pata tha... thanks for the information ❤️
Thank you bhiaya for making us hear this craziest news.
I use python for dsa , do i get more time exceeded errors while running the code compared to cpp or java
is it required or recommended to use "
" for the people who doesn't do competitive program?
Thankyou so much bro for sharing this information bcs I don't know about it.
THX😍
this video gave rise to TLE Eliminators :)
xD
Was aware of that
works faster than endl but not exepected that there is a huge diff between time limit..
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.
@@PriyanshAgarwal Actually it happened with me at Codechef, where precomputation was giving TLE, I endup skiping the question.
very helpful thanks!
Thank alot man👍
Very useful!
Bro can you help me develop logic?im only able to solve div2 a and div3 a,b,c
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.
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...
Nice video, though in interactive problems one might want to go with endl.
Totally agree. I myself use endl for interactive problems
Actually u have to go with endl or flush else you will get idleness error
What is an interactve problem bro?
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)
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.
@@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
@@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
Where can I find the code?
@Priyansh Agarwal
Makes understandable videos on codeforces blog 😎
I'm here after reading ur blog ,this video is helpful
but don't know why that blog has more dislikes
Because I gave it a clickbait title initially 😂
@@PriyanshAgarwal ohh 😅
Bhaiya java ka istarha koi tips bataiye na...!!!!
Merko pata tha ki
taz hota hei par bhai ji itna taz sir dard hi ho gai ye sun ke to .🤣🤣
Thanks
Could you please give a solution for MLE also??
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.
@@PriyanshAgarwal Thanks
What about cin/cout and scanf/printf?
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
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
This is good for saving time while working on projects. But I don't think using endl in cp will cause you tle
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.
:orz: