Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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 CSVReader::readCSV(string csvFilename) { vector entries;

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 CSVReader::tokenise(string csvLine, char separator) { vector tokens; signed int start, end; string token; start = csvLine.find_first_not_of(separator, 0); do { end = csvLine.find_first_of(separator, start);

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 tokens) { double price, amount;

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

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Pro Database Migration To Azure Data Modernization For The Enterprise

Authors: Kevin Kline, Denis McDowell, Dustin Dorsey, Matt Gordon

1st Edition

1484282299, 978-1484282298

More Books

Students also viewed these Databases questions