编译器版本
PS C:\Users\ChenH> g++ --version
g++.exe (Rev1, Built by MSYS2 project) 14.2.0
Copyright (C) 2024 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
编译选项:-O2 -std=c++14
#include<bits/stdc++.h>
int a[10000000];
int main(){
freopen("1.txt","w",stdout);
srand(time(0));
for(int i=0;i<10000000;i++){
a[i]=rand();
}
for(int i=1;i<10000000;i++){
printf("%d",a[i]);
}
}
--------------------------------
Process exited after 4.187 seconds with return value 0
请按任意键继续. . .
#include<bits/stdc++.h>
int a[10000000];
int main(){
//std::ios::sync_with_stdio(0);
freopen("1.txt","w",stdout);
srand(time(0));
for(int i=0;i<10000000;i++){
a[i]=rand();
}
for(int i=1;i<10000000;i++){
std::cout<<a[i];
}
}
--------------------------------
Process exited after 2.667 seconds with return value 0
请按任意键继续. . .
--------------------------------
Process exited after 1.162 seconds with return value 0
请按任意键继续. . .
求助:手贱试了一下printf的速度,结果比cout不关同步还慢,为什么,考场上到底该用哪个?