Please don't use stream i/o methods in C++ (ie. cin, cout) because they tend to be extremely slow.
created
last reply
- 24
replies
- 4.3k
views
- 22
users
- 2
likes
- 4
links
Please don't use stream i/o methods in C++ (ie. cin, cout) because they tend to be extremely slow.
They are much faster in 3.4. See for example this thread:
gcc.gnu.org/ml/libstdc++/2004-06/msg00271.html123
You're right, our tests also proved there's something wrong with (our?) g++-3.3. I think it's not exactly that they made some great improvements in 3.4: 2.95 has similar speed as 3.4.
Anyway, I was planning to switch spoj C/C++ compilers soon, probably also using stlport instead of STL from gcc (but it still needs some testing).
I found this thread rather interesting on this topic:
daniweb.com/techtalkforums/thread40450.html28
I found this to be the most interesting answers:
">and scanf() and printf() from their prototype know what kind of variables to expect.
And in what way do you think that the runtime type selection from a format string with scanf/printf is faster than selecting an overloaded function at compile time?"
any comments about this thread?
My logic tells me that iostream should be faster than printf, since how the arguments are parsed(run-time vs compile-time).
and to back it up: quora.com/C++-programming-la ... ME&share=117
I almost always use printf/scanf, because they are a lot faster. When my code gets TLE and the time complexity of the solution is similar to the complexity of the input, I use getchar_unlocked() and parse the input by myself. Even scanf -- faster than cin -- is still too slow. In SPOJ BR I got 0.5s in a problem with scanf, 0.17s with getchar and 0.02s with getchar_unlocked().
It means that, if the input is O(n²) and the expected solution is O(n²), maybe you can solve it in O(n² lg n) time just by parsing the input yourself (so, you don't spend much time with the input and get a lot of extra time for the solution). PS: I don't use atoi. Converting to integer WHILE reading the input is faster... for each algarism c you read, you update your integer to: x = 10*x + (c-'0').
Dobry wieczór. Brałem pod uwagę wszystko co było napisane na forum. Ktoś pomoże?
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main() {
int ile,x,y,z,i;
float miesiac;
scanf("%d",&ile);
for(i=0;i<ile;i++){
scanf("%d",&x);
scanf("%d",&y);
scanf("%d",&z);
miesiac=x+y;
miesiac=miesiac-z*y;
miesiac=miesiac/(z-1);
miesiac=miesiac*12;
miesiac=abs(miesiac);
miesiac=round(miesiac);
printf("%.0f",miesiac);
}
return 0;
}