读写挂与读写速度对比……

mac2022-06-30  12

滑动窗口因为读入太慢超时……于是做了这么个实验

#include<bits/stdc++.h> using namespace std; FILE *f; void write(int x) { if(x<0) { fputc('-',f); write(-x); return; } if(x>9) write(x/10); fputc(x+'0',f); } int main() { srand(time(NULL) ^ (unsigned long long)new char()); for(register int i=1; i<=10000000; i*=10) { f= fopen("in.txt", "w"); write(i); fputc('\n', f); for(int _=0; _<i; _++) { write(rand()*rand()+rand()+rand()); fputc('\n', f); } fclose(f); printf("###%d\n", i); putchar('.'); system("ConsolePauser fastio < in.txt > out.txt"); putchar('.'); system("ConsolePauser scanf < in.txt > out2.txt"); if (system("fc out.txt out2.txt")) { putchar('!'); putchar('\n'); } } return 0; }

fastio:

#include<bits/stdc++.h> using namespace std; #define REP(r,x,y) for(register int r=(x); r<(y); r++) #define REPE(r,x,y) for(register int r=(x); r<=(y); r++) #ifdef sahdsg #define DBG(...) printf(__VA_ARGS__) #else #define DBG(...) #endif #define MAXN 10000007 typedef long long LL; template <class T> inline void read(T& x) { char c=getchar(); int f=1; x=0; while(!isdigit(c)&&c!='-')c=getchar(); if(c=='-')f=-1,c=getchar(); while(isdigit(c)) { x=x*10+c-'0'; c=getchar(); } x*=f; } void write(int x) { if(x<0) { putchar('-'); write(-x); return; } if(x>9) write(x/10); putchar(x+'0'); } int main() { int n; read(n); while(0<n--) { int k; read(k); write(k); putchar('\n'); } return 0; }

 scanf+printf:

#include<bits/stdc++.h> using namespace std; #define REP(r,x,y) for(register int r=(x); r<(y); r++) #define REPE(r,x,y) for(register int r=(x); r<=(y); r++) #ifdef sahdsg #define DBG(...) printf(__VA_ARGS__) #else #define DBG(...) #endif #define MAXN 10000007 typedef long long LL; template <class T> inline void read(T& x) { char c=getchar(); int f=1; x=0; while(!isdigit(c)&&c!='-')c=getchar(); if(c=='-')f=-1,c=getchar(); while(isdigit(c)) { x=x*10+c-'0'; c=getchar(); } x*=f; } void write(int x) { if(x<0) { putchar('-'); write(-x); return; } if(x>9) write(x/10); putchar(x+'0'); } int main() { int n; scanf("%d", &n); while(0<n--) { int k; scanf("%d", &k); printf("%d\n", k); } return 0; }

 

 实验结果(单位:秒)

 110100$10^3$$10^4$$10^5$$10^6$$10^7$fastio0.045360.09420.10090.12420.14960.33422.03920.07scanf+printf0.053180.092670.072240.093990.17961.37410.57105.9cin+cout(not sync with stdio)0.69950.1330.061710.12240.29372.40423.91254.9

大于$10^5$就要注意用fastio……流输入无论如何都很慢,上面均是O3的结果

转载于:https://www.cnblogs.com/sahdsg/p/10486042.html

相关资源:JAVA上百实例源码以及开源项目
最新回复(0)