Hi,
couple a days ago I have asked the question "How to read the file for which i have no clue how large it is". But i did not know how inefficient will i be when i write it down. So in the following section i give you pseudo-codes of two reading systems (c and c++) and hope that you can help me to make it faster.
C function that reads the file of unknown size:
Code:
while((c = read(fd,buf,BUFSIZ)) > 0){
if(s->len + c > maxLen){
if(maxLen >= BUFSIZ)
maxLen *= 2;
else
maxLen = BUFSIZ + 1;
s->seq = (char *)realloc(s->seq,maxLen*sizeof(char));
}else if(buf[i] != '\n'){
s->seq[s->len] = buf[i];
s->len++;
}
}
read time:
time ./cread -i test > tmp
real 0m0.169s
user 0m0.120s
sys 0m0.030s
C++ function that reads the file of unknown size:
Code:
while (std::getline(infile, line, '\n')){
string.push_back(line);
}
time ./cppread -i test > tmp
real 0m4.692s
user 0m4.650s
sys 0m0.030s
Now this was tested under gcc 4.4.3 ubuntu on a file that has 1100000 characters. what i am dealing with are files that are 100x larger that that and i cannot invest 3h loading the file and 20 min computing. Is there faster way to load files of unknown size?
Please help
baxy
P.S.
there might be some spelling errors in the code.