注意:这不是现有std::ios::sync_with_stdio(false)
问题的重复。我已经经历了所有这些,但我无法cout
像printf
. 示例代码和证据如下所示。
我有三个源代码文件:
// ex1.cpp
#include <cstdio>
#include <chrono>
int main()
{
auto t1 = std::chrono::high_resolution_clock::now();
for (int i = 0; i < 10000000; i++) {
printf("%d\n", i);
}
auto t2 = std::chrono::high_resolution_clock::now();
auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(t2 - t1);
fprintf(stderr, "%lld\n", duration.count());
}
// ex2.cpp
#include <iostream>
#include <chrono>
int main()
{
auto t1 = std::chrono::high_resolution_clock::now();
for (int i = 0; i < 10000000; i++) {
std::cout << i << '\n';
}
auto t2 = std::chrono::high_resolution_clock::now();
auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(t2 - t1);
std::cerr << duration.count() << '\n';
}
// ex3.cpp
#include <iostream>
#include <chrono>
int main()
{
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
auto t1 = std::chrono::high_resolution_clock::now();
for (int i = 0; i < 10000000; i++) {
std::cout << i << '\n';
}
auto t2 = std::chrono::high_resolution_clock::now();
auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(t2 - t1);
std::cerr << duration.count() << '\n';
}
我不会在我的代码中混用cstdio
,iostream
所以我使用的 hacksex3.cpp
对我来说没问题。
我在带有固态驱动器的 macOS 上使用 clang++ 编译它们。
clang++ -std=c++11 -O2 -Wall -Wextra -pedantic ex1.cpp -o ex1
clang++ -std=c++11 -O2 -Wall -Wextra -pedantic ex2.cpp -o ex2
clang++ -std=c++11 -O2 -Wall -Wextra -pedantic ex3.cpp -o ex3
现在我运行它们并计时。
$ time ./ex1 > out.txt
1282
real 0m1.294s
user 0m1.217s
sys 0m0.071s
$ time ./ex1 > out.txt
1299
real 0m1.333s
user 0m1.221s
sys 0m0.072s
$ time ./ex1 > out.txt
1277
real 0m1.295s
user 0m1.214s
sys 0m0.070s
$ time ./ex2 > out.txt
3102
real 0m3.371s
user 0m3.037s
sys 0m0.075s
$ time ./ex2 > out.txt
3153
real 0m3.164s
user 0m3.073s
sys 0m0.075s
$ time ./ex2 > out.txt
3136
real 0m3.150s
user 0m3.051s
sys 0m0.077s
$ time ./ex3 > out.txt
3118
real 0m3.513s
user 0m3.045s
sys 0m0.080s
$ time ./ex3 > out.txt
3113
real 0m3.124s
user 0m3.042s
sys 0m0.077s
$ time ./ex3 > out.txt
3095
real 0m3.107s
user 0m3.029s
sys 0m0.073s
即使我将输出重定向到/dev/null
. 结果也与-O3
优化级别非常相似。
两者ex3
和ex2
都比ex1
? 是否有可能以任何方式使用std::cout
提供与 相当的速度printf
?