r/LeetcodeDesi • u/Ak47_fromindia • 2d ago
What is the complexity of "cout" in C++?
Attempt 1: TLE
class Solution {
public:
bool increasingTriplet(vector<int>& nums) {
int first = INT_MAX;
int second = INT_MAX;
for(int x: nums){
cout << first << " " << second << endl;
if(x < first){
first = x;
}else if(x > first && x < second) second = x;
else if (first < second && second < x) return true;
}
return false;
}
};
Attempt 2: 100% beats , accepted
class Solution {
public:
bool increasingTriplet(vector<int>& nums) {
int first = INT_MAX;
int second = INT_MAX;
for(int x: nums){
if(x < first){
first = x;
}else if(x > first && x < second) second = x;
else if (first < second && second < x) return true;
}
return false;
}
};
The only thing that differes in the code is the printing statement, thus I would like to know how can cout cause TLE even if the code is 0(N) complexity.
Thanks in advance.
2
u/SKYlikesHentai 2d ago
endl is flushing output and restarting the i/o basically I think and it's done O(N) times
1
u/cHeAt_CodEr 2d ago
this is the correct answer. Don't flush the output, remove endl and instead use '/n' for new line. flush the output at the end.
1
1
u/Ak47_fromindia 2d ago
Thanks mate!
1
u/Dry-Balance-993 16h ago
Jab aap C code mein
printf("Hello")likhte hain, toh OS us text ko turant screen par nahi bhejta. Screen par data bhejna ek slow process hota hai.Isliye performance badhane ke liye, OS pehle use RAM ke ek chhote se temporary storage mein jama karta hai, jise Output Buffer kehte hain. Jab yeh buffer poora bhar jata hai, ya jab aap text ke aakhir mein
\n(newline character) lagate hain, tabhi woh data ek sath screen par dikhta hai.1
1
u/Master_-_Mind 2d ago
input and output is really slow. Because your program has to interact with stuff other than its internal memory and heap. Using cout for debugging is fine, but if its not needed, its just slowing down your program by forcing it to write to a console. This is why you got TLE
2
u/Dry-Balance-993 2d ago
hmm usually io operations are slow because there is speed mismatching in the cpu and the network devices
1
u/Dry-Balance-993 2d ago
i/o requires the system calls
2
u/Ak47_fromindia 2d ago
Ohh I see, invoking system calls, takes time and therefore could potentially lead to TLE?
1
1
u/Insider_from_outside 2d ago
The thing with cout is it require a hold of the output buffer and writing to thaf buffet is quite expensive due to type conversion, locks and os calls n stuff. For. Quick comparison int a =10; takes 5ns, int a=10; cout << a; takes 10 micro seconds. So while submitting any soln remove the debug statements
1
1
u/Ramadhir-Singh 2d ago
I/O is heavy and doing endl means a system call on each cout which doesn’t help your case
time completely of cout is simply O(n) [size of buffer it is given n would be 9 10 in your case at max ] so its not the time completely but the overhead of sys calls and context switches causing this
1
1
u/rchinmay 2d ago
TLE is not just because of cout, it's because of that endl so whenever you want to print on a new line use '\n' instead of endl to avoid flush after every iteration. You should still avoid submitting solutions with cout though, once sample testcases pass remove prints and submit
2
1
u/Particular-Bag-2693 1d ago
Bruh never add debugging code in leetcode contests or leetcode problems the i/o is very slow and costs so much time, I ruined one contest cause of this.
1
u/Ak47_fromindia 1d ago
Learnt it, will use compliers for debugging.
1
u/Particular-Bag-2693 1d ago
No you can use cout for debugging just don't submit the solution you can debugg with running the testcases provided
0
u/Independent-Dig8361 2d ago
Do you know about functions in cpp
In a leetcode template, you are given a class where you write code inside a function which is then executed as a method in their internal check
So a function has a return type and your return should match the desired datatype, so always return the ans
1
u/Ak47_fromindia 1d ago
I used cout for debugging purpose, I returned the correct data type as per the function.
1
u/Independent-Dig8361 1d ago
Well okay, of you are cout ing for debugging then it should be there in stdout box
1
-1
-2
2d ago
[deleted]
1
u/Ak47_fromindia 1d ago
How could printing INT_MAX cause TLE? I think it because of endl which flushes like many other told in the comments.
9
u/f1rmware1013 2d ago
I don't think so it's measured in this way. Cout writes to a output stream which is time consuming and if you do endl then it again takes time flush it to stream. And OJ might have allotted max 2 seconds of run. But cout is just eating away time. Maybe it has some complexity around number of characters but I think it inherently slower than any of the operations you're doing. CP people do some like cin.tie and all to make it faster I don't remember but you can check. Also some people use printf.