Location>code7788 >text

scanf, cin and its optimization, fast read performance test

Popularity:660 ℃/2024-07-23 14:54:20

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.

image

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

image

\(328\) millisecond

No optimization cin

image

\(577\) millisecond

Optimized cin

image

\(81\) millisecond

Quick read (disregarding negative numbers)

image

\(155\) millisecond

output test

Output each number followed by a newline

printf

image

\(989\) millisecond

No optimization cout(endl)

image

\(1447\) millisecond

Optimized cout(endl)

image

\(1386\) millisecond

No optimization cout(\n)

image

\(61\) millisecond

Optimized cout(\n)

image

\(73\) millisecond

quick write

image

\(105\) millisecond

summarize

importation

If you don't want to turn off read/write synchronization use fast read, in any case don't use nativecinscanf 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.