I am reading a file and saving the data into a vector
. I cannot use arrays
because the data size is not fixed. The file size is about 300kb and could go up to 600kb. Currently, this takes about 5 - 8 seconds to read/save.
I would like to know what is slowing down my read/copy method and how it might be improved?
Sample data:
0000:4000 94 45 30 39 36 39 74 00 00 00 00 50 00 00 00 27 some other info here
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;
}