缓慢的文件读取和复制到内存-C++
Slow file reading and copying into memory - C++
我正在读取一个文件并将数据保存到vector
中。我无法使用arrays
,因为数据大小不是固定的。文件大小约为300kb,最高可达600kb。目前,这大约需要5-8秒来读取/保存。
我想知道是什么减缓了我的阅读/复制方法,以及如何改进它?
样本数据:
0000:4000 94 45 30 39 39 74 00 00 00 00 50 00 00 00 27部分其他信息在这里
int SomeClass::Open ()
{
vector <unsigned int> memory; // where the data will be stored
file.open("c:\file.txt",ios::in);
regex addressPattern("0000:(\d|[a-z]){4}"); // used to extract the address from a string
regex dataPattern("( (\d|[a-z]){2}){16}"); // used to extract the data from a string
smatch match;
string str; // where each line will be stored
string data; // where the data found in each line will be stored
int firstAddress = -1; // -1 = address not been found
unsigned int sector = 0;
unsigned int address = 0;
while(getline(file,str)){
if(regex_search(str,match,addressPattern) && firstAddress == -1){
sector = std::stoul(match.str().substr(0,3),nullptr,16);
address = std::stoul(match.str().substr(5),nullptr,16);
firstAddress = address;
}
if(regex_search(str,match,dataPattern)){
std::istringstream stream(str);
string data; // used to store individual byte from dataString
while(stream >> data){
unsigned int c = std::stoul(data,nullptr,16); // convertion from hex to dec
memory.insert(memory.end(),c);
}
}
}
return 0;
}
这似乎是意料之中的事。使用Boost::Progress
或ctime
来隔离代价高昂的指令。
矢量是以数组的方式用连续内存实现的,所以您不应该看到太多(如果有的话)速度减慢。对于600kb的文件来说,文件IO时间可能是最短的——我想它在打开时会缓存到内存中。您可以使用file.open的ios::binary
模式标志将整个文件缓存到内存中,但您必须反序列化每一行——这是getline抽象的代价。
尽管如此,编译器在优化IO和向量方面做得非常好。瓶颈可能是正则表达式的构造(甚至正则表达式匹配),这是必要的&复杂的为每个正则表达式生成确定性有限状态自动机:What';平均Regex算法的时间复杂性是什么?。
Regex的功能非常强大,但复杂而缓慢。
由于您的格式是完全静态的(固定的数字数量和中间的固定分隔符),您可以自己实现转换,逐个读取字符。这不会很复杂。
例如,读取所有十六进制数字,并检查空格和分号:
while(getline(file,str))
{
if(str.size()>=57)
{
int sector = hexToInt(str.data(), 4);
int address = hexToInt(str.data()+5, 4);
bool ok = ok && (sector==0) && (address>=0);
ok = ok && str[4] == ':';
int bytes[16];
for(int i=0;i<16;++i)
{
bytes[i] = hexToInt(str.data()+10+3*i, 2);
ok = ok && (str[9+3*i]==' ') && (bytes[i]>=0);
}
}
//Etc...
}
检查和转换十六进制数字的功能:
int hexCharToDigit(char c)
{
if(c>='0' && c<='9')
{
//Decimal digit
return (int)(c-'0');
}
else if (str[i]>='a' && str[i]<='f')
{
//Hexadecimal lower case letter
return (int)(c-'a')+10;
}
else if (str[i]>='A' && str[i]<='F')
{
//Hexadecimal upper case letter
return (int)(c-'A')+10;
}
else
{
//Char is not a hex digit
return -1;
}
}
用于检查n位十六进制并将其转换为int的函数:
int hexToInt(const char * chr, int size)
{
assert(size<8);
int result= 0;
for(int i=0;i<size;++i)
{
int hexDigit = hexCharToDigit(chr[i]);
if(hexDigit>=0)
{
//Valid hexadecimal digit
result = result << 4;
result += hexDigit;
}
else
{
//Char is not a hex digit as expected
return -1;
}
}
return result;
}
相关文章:
- 将字符串存储在c++中的稳定内存中
- C++ 指针的内存地址和指向数组的内存地址如何相同?
- Win32编译器选项和内存分配
- 当vector是tje全局变量时,c++中vector的内存管理
- 带内存和隔离功能的SQLite
- 是否可以通过C++扩展强制多个python进程共享同一内存
- 迭代时从向量和内存中删除对象
- 在C++中打印指向不同基元数据类型的指针的内存地址
- 这个指针和内存代码打印是什么?我不知道是打印垃圾还是如何打印我需要的值
- 多个文件的内存分配错误"在抛出 'std :: bad_alloc' what (): std :: bad_alloc 的实例后终止调用" [C++]
- 为什么示例代码访问IUnknown中已删除的内存
- 如何在C++类内存结构中创建"spacer"?
- 从构造函数抛出异常时如何克服内存泄漏
- malloc() 可能出现内存泄漏
- 如何理解将半精度指针转换为无符号长指针和相关的内存对齐
- 在调用FreeLibrary后,释放动态链接到具有相同版本的CRT堆的DLL的内存
- 如何针对特定情况调试和修复此双自由内存损坏问题
- 类型总是使用其大小存储在内存中吗
- 有没有一种方法可以测量c++程序的运行时内存使用情况
- 有没有一种方法可以使用placement new将堆叠对象分配给分配的内存