社区讨论
求助:手贱试了一下printf的速度,结果比cout还慢
学术版参与者 5已保存回复 5
讨论操作
快速查看讨论及其快照的属性,并进行相关操作。
- 当前回复
- 5 条
- 当前快照
- 1 份
- 快照标识符
- @m2pmia1c
- 此快照首次捕获于
- 2024/10/26 11:50 去年
- 此快照最后确认于
- 2025/11/04 16:06 4 个月前
如题
编译器版本
CPPPS 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++14printf:
CPP#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]);
}
}
resualt:
CPP--------------------------------
Process exited after 4.187 seconds with return value 0
请按任意键继续. . .
cout:
CPP#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];
}
}
resualt(不关同步):
CPP--------------------------------
Process exited after 2.667 seconds with return value 0
请按任意键继续. . .
关同步:
CPP--------------------------------
Process exited after 1.162 seconds with return value 0
请按任意键继续. . .
求助:手贱试了一下printf的速度,结果比cout不关同步还慢,为什么,考场上到底该用哪个?
回复
共 5 条回复,欢迎继续交流。
正在加载回复...