C++ 为什么C<;标准h>;FILE*fread()比Win32 ReadFile()快吗?
使用以下三种技术比较读取文件:C++ 为什么C<;标准h>;FILE*fread()比Win32 ReadFile()快吗?,c++,c,winapi,file-io,C++,C,Winapi,File Io,使用以下三种技术比较读取文件: C文件* Win32CreateFile()/ReadFile() Win32内存映射 我注意到1比2快,3是最快的 e、 g.从最快到最慢排序,对于处理900MB测试文件,我得到以下结果: Win32内存映射:821.308毫秒 C文件(文件*):1779.83毫秒 Win32文件(CreateFile):3649.67毫秒 为什么C技术比Win32ReadFile()访问更快?我希望原始Win32 API的开销比CRT少。我错过了什么 可编译的测试C++源代码
文件*
CreateFile()
/ReadFile()
技术比Win32ReadFile()访问更快?我希望原始Win32 API的开销比CRT少。我错过了什么
可编译的测试C++源代码如下:
编辑
我使用4KB的读取缓冲区重复了测试,并使用三个不同的文件(具有相同的内容)来避免缓存效应,这可能会扭曲性能度量,现在结果与预期一致。
例如,对于大约400 MB的文件,结果如下:
Win32内存映射:305.908毫秒
Win32文件(CreateFile):451.402毫秒
C文件(文件*):460.579毫秒
////////////////////////////////////////////////////////////////////////////////
//使用C文件*、Win32 CreateFile和Win32内存映射读取测试文件。
////////////////////////////////////////////////////////////////////////////////
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
//------------------------------------------------------------------------
//性能(速度)测量
//------------------------------------------------------------------------
长计数器()
{
大整数李;
QueryPerformanceCounter(&li);
返回li.QuadPart;
}
长频率
{
大整数李;
查询性能频率(&li);
返回li.QuadPart;
}
无效打印时间(恒长开始、恒长结束、,
常量字符*常量s)
{
std::cout您确定测试正确吗?
如何计算磁盘位置、寻道时间、文件缓存等
stdio和win32最终对Windows内核进行相同的调用以打开该文件
mmap的做法稍有不同,因为它可以保留实际读取数据的时间,直到数据被使用为止-如果您有固定的文件大小和性能问题,mmap是一个很好的选择我所获得的最快磁盘访问速度是使用ReadFile
。但是我专门打开了带有标志的文件,以满足我的磁盘访问和cachin要求如果你只是一字不差地使用它,比较起来有点站不住脚
您应该阅读更多有关该函数以及CreateFile
的内容。您会发现可以将扇区大小的块(倍数)中的数据读取到扇区对齐内存中。然后您将执行fread
正如其他人所说,fread
正在进行自己的缓冲。使用ReadFile
的缓冲实现仍然需要工作
查看MSDN。所有信息都在那里。具体来说,请点击此处:
使用内存映射文件时,无需将文件内容复制到应用程序中-它作为虚拟内存的一部分直接从操作系统映射到应用程序中,因此当您访问文件内容时,只需将其直接读入映射内存中的页面即可
如果您在使用Win32 API时正确地完成了这项工作,那么C stdio应该会更快,因为调用中的开销更少。但是,很可能您没有在系统调用开销和“缓冲区太大,因此读取时间比需要的时间长”之间取得理想的平衡。我建议您尝试使用4K或8K(甚至可能是32K)作为Win32 API功能中的缓冲区—理想的缓冲区大小是4K的倍数,因为内存页(通常)是4KB。对API的调用越少,开销就越小,但您不想走得太远
[前几天我在Linux上做了一些类似的测试,发现了类似的结果——根据我在那里的经验:每个测试使用不同的文件,否则文件系统缓存将有助于以后运行的测试!。刚刚在我的机器上运行了一些测试,结果表明增加缓冲区大小实际上会提高性能:
C <stdio.h> file (FILE*): 1431.93 ms
Bufsize: 0
Count of 'x' : 3161882
Win32 file (CreateFile): 2289.45 ms
Bufsize: 1024
Count of 'x' : 3161882
Win32 file (CreateFile): 1714.5 ms
Bufsize: 2048
Count of 'x' : 3161882
Win32 file (CreateFile): 1479.16 ms
Bufsize: 4096
Count of 'x' : 3161882
Win32 file (CreateFile): 1328.25 ms
Bufsize: 8192
Count of 'x' : 3161882
Win32 file (CreateFile): 1256.1 ms
Bufsize: 16384
Count of 'x' : 3161882
Win32 file (CreateFile): 1223.54 ms
Bufsize: 32768
Count of 'x' : 3161882
Win32 file (CreateFile): 1224.84 ms
Bufsize: 65536
Count of 'x' : 3161882
Win32 file (CreateFile): 1212.4 ms
Bufsize: 131072
Count of 'x' : 3161882
Win32 file (CreateFile): 1238.09 ms
Bufsize: 262144
Count of 'x' : 3161882
Win32 file (CreateFile): 1209.2 ms
Bufsize: 524288
Count of 'x' : 3161882
Win32 file (CreateFile): 1223.67 ms
Bufsize: 1048576
Count of 'x' : 3161882
Win32 file (CreateFile): 1349.98 ms
Bufsize: 2097152
Count of 'x' : 3161882
Win32 memory mapping: 796.281 ms
Bufsize: 0
Count of 'x' : 3161882
C文件(文件*):1431.93毫秒
Bufsize:0
“x”的计数:3161882
Win32文件(CreateFile):2289.45毫秒
Bufsize:1024
“x”的计数:3161882
Win32文件(CreateFile):1714.5毫秒
Bufsize:2048
“x”的计数:3161882
Win32文件(CreateFile):1479.16毫秒
Bufsize:4096
“x”的计数:3161882
Win32文件(CreateFile):1328.25毫秒
Bufsize:8192
“x”的计数:3161882
Win32文件(CreateFile):1256.1毫秒
Bufsize:16384
“x”的计数:3161882
Win32文件(CreateFile):1223.54毫秒
尺寸:32768
“x”的计数:3161882
Win32文件(CreateFile):1224.84毫秒
Bufsize:65536
“x”的计数:3161882
Win32文件(CreateFile):1212.4毫秒
Bufsize:131072
“x”的计数:3161882
Win32文件(CreateFile):1238.09毫秒
Bufsize:262144
“x”的计数:3161882
Win32文件(CreateFile):1209.2毫秒
Bufsize:524288
“x”的计数:3161882
Win32文件(CreateFile):1223.67毫秒
Bufsize:1048576
“x”的计数:3161882
Win32文件(CreateFile):1349.98毫秒
Bufsize:2097152
“x”的计数:3161882
Win32内存映射:796.281毫秒
Bufsize:0
“x”的计数:3161882
Visual Studio 2012调试器中的某些步骤显示FILE*方法的缓冲区大小为4096字节,至少在我的计算机上是这样。(正如其他人所说,除非您从控制台读取,否则它也会调用ReadFile
)
同样有趣的是,大的缓冲区会略微降低性能。将新的
操作符移到测试之外也不能解决这个问题
首先是内存映射
C <stdio.h> file (FILE*): 1431.93 ms
Bufsize: 0
Count of 'x' : 3161882
Win32 file (CreateFile): 2289.45 ms
Bufsize: 1024
Count of 'x' : 3161882
Win32 file (CreateFile): 1714.5 ms
Bufsize: 2048
Count of 'x' : 3161882
Win32 file (CreateFile): 1479.16 ms
Bufsize: 4096
Count of 'x' : 3161882
Win32 file (CreateFile): 1328.25 ms
Bufsize: 8192
Count of 'x' : 3161882
Win32 file (CreateFile): 1256.1 ms
Bufsize: 16384
Count of 'x' : 3161882
Win32 file (CreateFile): 1223.54 ms
Bufsize: 32768
Count of 'x' : 3161882
Win32 file (CreateFile): 1224.84 ms
Bufsize: 65536
Count of 'x' : 3161882
Win32 file (CreateFile): 1212.4 ms
Bufsize: 131072
Count of 'x' : 3161882
Win32 file (CreateFile): 1238.09 ms
Bufsize: 262144
Count of 'x' : 3161882
Win32 file (CreateFile): 1209.2 ms
Bufsize: 524288
Count of 'x' : 3161882
Win32 file (CreateFile): 1223.67 ms
Bufsize: 1048576
Count of 'x' : 3161882
Win32 file (CreateFile): 1349.98 ms
Bufsize: 2097152
Count of 'x' : 3161882
Win32 memory mapping: 796.281 ms
Bufsize: 0
Count of 'x' : 3161882