I have written a CSV parser to read in 12609000 lines of CSV data. Each line has 16 fields of both string and double type data. The following code takes about 40 seconds to parse the data. I would like to know if there is a better way of doing this which can make it even faster. I have seen recommendations to load all of the CSV data from the file at once into memory and then perform the parsing as in here.
I have tried it but didn't make too much of a difference. I am posting the core of my code which takes the longest time, about 37 out of the 40 seconds.
// outBuffer is a (char*) to a char array containing the CSV
// string I load from the file. outBufferSize has the length
// of outBuffer.
// Get number of lines in the file.
int num_rows = 0;
for(int i=0;i<outBufferSize;i++) {
if (outBuffer[i]=='\n')
num_rows++;
}
// Assign required memory
lastPtr = calloc(num_rows,sizeof(double));
lastPtr = calloc(num_rows,sizeof(double));
lastPtr = calloc(num_rows,sizeof(double));
bidPtr = calloc(num_rows,sizeof(double));
askPtr = calloc(num_rows,sizeof(double));
volPtr = calloc(num_rows,sizeof(double));
highPtr = calloc(num_rows,sizeof(double));
lowPtr = calloc(num_rows,sizeof(double));
bidSizePtr = calloc(num_rows,sizeof(double));
askSizePtr = calloc(num_rows,sizeof(double));
lastSizePtr = calloc(num_rows,sizeof(double));
// Get the indices of end-of-line characters in outBuffer.
int *line_ends = (int*) calloc(num_rows,sizeof(int));
for(int i=0,j=0;i<outBufferSize;i++){
if(outBuffer[i]=='\n')
line_ends[j++] = i;
}
int line = 0;
int line_begin=0;
char token[4096];
while(line<num_rows){
memcpy(token,&outBuffer[line_begin],(size_t)line_ends[line]-line_begin);
line_begin = line_ends[line]+1;
sscanf(token,"%[^,],%[^,],%f,%f,%f,%f,%f,%f,%f,%f,%[^,],%f,%f,%f,%f,%f",
time,symbol,&last,&bid,&ask,&volume,&y_close,&open,&high,&low,trade_time,&avg_vol,&y_short,&bid_size,&ask_size,&last_size);
lastPtr[line] = last;
bidPtr[line] = bid;
askPtr[line] = ask;
volPtr[line] = volume;
highPtr[line] = high;
lowPtr[line] = low;
bidSizePtr[line] = bid_size;
askSizePtr[line] = ask_size;
lastSizePtr[line] = last_size;
line++;
}