Question
How do I optimise my code so that it doesn't take so long to open a CSV File with more than 1 million entries? #include
How do I optimise my code so that it doesn't take so long to open a CSV File with more than 1 million entries?
#include "CSVReader.h"
CSVReader::CSVReader() {
}
vector
ifstream csvFile{ csvFilename }; string line; if (csvFile.is_open()) { while (getline(csvFile, line)) { try { OrderBookEntry obe = stringsToOBE(tokenise(line, ',')); entries.push_back(obe); }
catch (const exception& e) { continue; } } }
else { cout << "Problem opening file " << csvFilename << endl; }
csvFile.close(); return entries; }
vector
if (start == csvLine.length() || start == end) break;
if (end >= 0) token = csvLine.substr(start, end - start);
else token = csvLine.substr(start, csvLine.length() - start);
tokens.push_back(token); // Save this token
start = end + 1; } while (end > 0); // While (end > 0) continue loop condition
return tokens; }
OrderBookEntry CSVReader::stringsToOBE(vector
if (tokens.size() != 5) // Bad { throw exception{}; }
// we have 5 tokens try { price = stod(tokens[3]); amount = stod(tokens[4]); }
catch (const exception& e) { throw; }
OrderBookEntry obe{ price, amount, tokens[0], tokens[1], OrderBookEntry::stringToOrderBookType(tokens[2]) };
return obe; }
OrderBookEntry CSVReader::stringsToOBE(string priceString, string amountString, string timestamp, string product, OrderBookType orderType) { double price, amount;
try { price = stod(priceString); amount = stod(amountString); }
catch (const exception& e) { cout << "CSVReader::stringsToOBE Bad float! " << priceString << endl; cout << "CSVReader::stringsToOBE Bad float! " << amountString << endl; throw; }
OrderBookEntry obe{ price, amount, timestamp, product, orderType };
return obe; }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started