为什么它更快地编写与Stdout一起频率的文件
Why it is faster writing a file which is freopened with stdout?
在Windows上执行时,此测试代码:
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <time.h>
#include <assert.h>
int main() {
// The clock() function returns an approximation of processor time used by the program.
// The value returned is the CPU time used so far as a clock_t;
// to get the number of seconds used, divide by CLOCKS_PER_SEC.
auto buf = new char[1048576]; // 1MB
auto cache = new char[512 * 1024];
// initialize the buffer
for (int i = 0; i < 1048576; ++i)
buf[i] = i;
auto fp_reopen = freopen("data_freopen.bin", "wb", stdout);
assert(fp_reopen != nullptr);
setvbuf(fp_reopen, cache, _IOLBF, 512 * 1024);
auto clock_begin = clock();
for (int i = 0; i < 1000; ++i) {
auto n = fwrite(buf, 1048576, 1, fp_reopen);
assert(n == 1);
}
fflush(fp_reopen);
auto clock_end = clock();
#ifdef _WIN32
freopen("CONOUT$", "w", stdout);
#else
freopen("/dev/tty", "w", stdout);
#endif
printf("write with freopen clocks elapsed: %zun", clock_end - clock_begin);
auto fp = fopen("data_fopen.bin", "wb");
assert(fp != nullptr);
setvbuf(fp, cache, _IOLBF, 512 * 1024);
clock_begin = clock();
for (int i = 0; i < 1000; ++i) {
auto n = fwrite(buf, 1048576, 1, fp);
assert(n == 1);
}
fflush(fp);
clock_end = clock();
fclose(fp);
printf("write with fopen clocks elapsed: %zun", clock_end - clock_begin);
delete[] buf;
delete[] cache;
getchar();
}
生成这些结果:
-
write with freopen clocks elapsed: 2767
-
write with fopen clocks elapsed: 8337
为什么?
您的问题很有趣,但是高度系统的:
- 在使用GCC和GLIBC的Linux上,我都会获得两个运行的时间
- 在OS/X上,带有Clang和Apple Libc,
fopen
时间似乎比freopen
的时间始终快一点。 - 您正在Windows上进行测试,因为
getchar()
的最终呼叫建议...不幸的是,我无法测试此系统以尝试交叉检查您的观察值。
Microsoft可能在其运行时库中做了一些奇怪的事情,但是您更有可能确实基于创建2个单独的1GB文件的创建。由于文件系统的状态,其缓存或其他特定于OS的原因,第二个文件可能比第一个文件所需的时间更长。您应该尝试通过关闭每个文件或以不同顺序运行测试来尝试删除此潜在的副作用。
相关文章:
- 如何将stdout重定向到stderr
- 如何将enable-if与模板参数和参数包一起使用
- 如何将PERF_AMPLE_READ与mmap一起使用
- 如何将两个不同矢量的同一位置的两个元素组合在一起
- 如何将C++中的库和头与MinGW一起使用
- 将--whole archive链接器选项与CMake和具有其他库依赖项的库一起使用
- 为什么我不能将 rand() 与数组的大小一起使用?
- 要与"if constexpr"一起使用的编译时消息(在预处理器之后)
- 不能将复制初始化与隐式转换的多个步骤一起使用
- 将fold表达式与std::一起用于两个元组
- spdlog标头仅与外部fmt一起使用.spdlog错误:'内部':不是'fmt'
- 将 std::allocate_shared 与多态资源分配器一起使用
- 为什么常量词在重载运算符中不与 ostream 对象一起使用<<?
- 将 OpenCV 与 CMAKE 中的项目一起构建为第三方库的正确方法
- 将 exprtk 与自定义类的对象一起使用
- 将 std::set 与基于键的比较器一起使用
- 将 C++ 类与 Rcpp 一起使用,从 C 或 R 修改它
- 如何将 Eigen::Ref 与 pybind11 一起使用?
- 如何将AERT_Allocate与 std:vector 一起使用
- 为什么它更快地编写与Stdout一起频率的文件