快速分析文件
Parse files the fast way?
本文关键字:文件 更新时间:2023-10-16
我正在写一个图库,它应该读取最常见的图格式。一种格式包含这样的信息:
e 4 3
e 2 2
e 6 2
e 3 2
e 1 2
....
我想解析这些行。我环顾了一下stackoverflow,可以找到一个巧妙的解决方案。我目前使用这样的方法(文件是一个fstream):
string line;
while(getline(file, line)) {
if(!line.length()) continue; //skip empty lines
stringstream parseline = stringstream(line);
char identifier;
parseline >> identifier; //Lese das erste zeichen
if(identifier == 'e') {
int n, m;
parseline >> n;
parseline >> m;
foo(n,m) //Here i handle the input
}
}
它运行得很好,正如预期的那样,但今天当我用巨大的图形文件(50mb+)测试它时,我震惊地发现这个函数是整个程序中最糟糕的瓶颈:
我用来解析该行的字符串流几乎占总运行时的70%,getline命令占25%。该程序的其余部分仅使用5%。
有没有一种快速读取这些大文件的方法,可能会避免慢速字符串流和getline函数?
您可以跳过对字符串的双重缓冲,跳过对单个字符的解析,并使用strtoll
解析整数,如下所示:
string line;
while(getline(file, line)) {
if(!line.length()) continue; //skip empty lines
if (line[0] == 'e') {
char *ptr;
int n = strtoll(line.c_str()+2, &ptr, 10);
int m = strtoll(ptr+1, &ptr, 10);
foo(n,m) //Here i handle the input
}
}
在C++中,strtoll
应该在<cstdlib>
包含文件中。
mmap文件并将其作为单个大缓冲区进行处理。
如果您的系统缺少mmap,您可以尝试将文件read
malloc
的缓冲区
理由:大部分时间都在从用户到系统的转换过程中,然后再调用C库。读取整个文件几乎消除了所有这些调用。
相关文章:
- .cpp和.h文件中的模板专用化声明
- 为什么两个不同的未命名名称空间可以共存于一个cpp文件中
- 文本文件中的单词链表
- CMake-按正确顺序将项目与C运行时对象文件链接
- 使用新行和不使用新行读取文件
- 在C++程序中输入的文本文件将不起作用,除非文本被复制和粘贴
- 挂起和取消挂起一个文件DLL
- 如何确定我已使用非编码文件到达 EOF?
- 命名空间中具有.h和.cpp文件的类
- 如何使用ndk-build.cmd构建Android.so文件
- 从包含m行的文件中提取n行,必要时(惰性地)重复该文件
- 读取文件并输入到矢量中
- 在C++中查找文件
- c++库的公共头文件中应该包含什么
- 用c++从输入文件中读取另一行
- Cppcheck生成xml转储文件
- 读取文件的最后一行并输入到链接列表时出错
- 无法编译 rtmidi 测试 cmidiin.cpp 文件, 非法指令
- 如何将内容数组写入文本文件?
- C++ Windows 驱动程序MSB3030无法复制该文件,因为它找不到