gpt4 book ai didi

c++ - 比 boost::file_mapping 更快的读取文件的方法?

转载 作者:可可西里 更新时间:2023-11-01 11:13:24 25 4
gpt4 key购买 nike

我正在编写一个对延迟敏感的应用程序,它在初始化时读取文本文件。我分析并重写了我所有的算法,这样我 85% 的执行时间来自以下几行:

boost::interprocess::file_mapping file(Path, read_only);
boost::interprocess::mapped_region data(file, read_only);

我在 Windows 上写这篇文章 - 有没有更快的方法将文件映射到内存中?便携性不是问题。

最佳答案

您可以只使用 Win32 的 native 函数,但我认为您不会节省很多,因为 boost 不会增加很多开销:

OFSTRUCT ofStruct;
ofStruct.cBytes=sizeof (OFSTRUCT);
HANDLE file=(HANDLE)OpenFile(fileName, &ofStruct, OF_READ);
if (file==INVALID_HANDLE_VALUE)
handle errors
else {
HANDLE map=CreateFileMapping(file, NULL, PAGE_READONLY, 0, 0, 0);
if (map==INVALID_HANDLE_VALUE)
handle errors
else {
const char *p=(const char *)MapViewOfFile(map, FILE_MAP_READ, 0, 0, 0));
if (p) {
// enjoy using p to read access file contents.
}
// close all that handles now...
}

关于c++ - 比 boost::file_mapping 更快的读取文件的方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13905789/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com