To give you an idea of the performance of the various C++ IO methods, this article was created.
The data taken for this test are all\(10^6\) No more than\(10^8\) Random positive integers.
test code:
#include<bits/stdc++.h>
using namespace std.
int x; int main()
int main(){
freopen("", "r",stdin); freopen("", "w",stdout); int main(){
int main(){ freopen("", "w",stdin); freopen("", "w",stdout);
double s=clock();//record start time
for(int i=1;i<=1000000;i++){
//Test content
}
cerr<<clock()-s<<"\n";//output the running time in the terminal
return 0; }
}
input test
scanf
\(328\) millisecond
No optimization cin
\(577\) millisecond
Optimized cin
\(81\) millisecond
Quick read (disregarding negative numbers)
\(155\) millisecond
output test
Output each number followed by a newline
printf
\(989\) millisecond
No optimization cout(endl)
\(1447\) millisecond
Optimized cout(endl)
\(1386\) millisecond
No optimization cout(\n)
\(61\) millisecond
Optimized cout(\n)
\(73\) millisecond
quick write
\(105\) millisecond
summarize
importation
If you don't want to turn off read/write synchronization use fast read, in any case don't use nativecin
。scanf
Neutral.
exports
Fast writing is much less optimized than fast reading, almost none.
normalprintf
Enough.
endl
line feedcin
Slow as hell, kill it and don't use it.
Recommended\n
Line breaks, optimization doesn't matter.
In summary, without turning off read/write synchronization it is recommended to use Fast Read+\(\texttt{cout}\)(\n line breaks), and turning off read/write synchronization is a no-brainer cin/cout.