2015年2月18日 星期三

A program to read CSV file with time elapsed reported


File:

"0#AU113554908=",6261,88,214
"![WTCL9700I6",4128,132,758
"/JNKO201504900.U",4007,115,575
"/2EH9675D5",4911,244,22


Code:
#include <iostream>
#include <fstream>
#include <iterator>
#include <sstream>
#include <vector>
#include <string>
#include <ctime>

using namespace std;

class CSVRow
{
public:
string const& operator[](size_t index) const
{
return vTokens[index];
}
size_t size() const
{
return vTokens.size();
}
void readNextRow(istream& str)
{
string line;
getline(str, line);

stringstream lineStream(line);
string token;

vTokens.clear();
while(getline(lineStream, token, ','))
{
vTokens.push_back(token);
}
}
private:
vector<string> vTokens;
};

istream& operator>>(istream& str, CSVRow& data)
{
data.readNextRow(str);
return str;
}

class Timer
{
string s;
clock_t t;
public:
Timer(string s="") : t(clock()), s(s) {}
~Timer()
{
cout << "Function " + s + " elapsed in seconds [" << double(clock() - t) / CLOCKS_PER_SEC << "]" << '\n';
};
};

class Instrument
{
public:
string name;
int pe;
int recordType;
int exchangeID;
Instrument():name(""), pe(0), recordType(0), exchangeID(0) {}
Instrument (const string & name, int pe, int recordType, int exchangeID)
: name(name), pe(pe), recordType(recordType), exchangeID(exchangeID) {}
};

void TestRouteine(const string & filepath, vector<Instrument> & vInstruments)
{
volatile Timer timer("ReadCSV");
vInstruments.reserve(40000000);
ifstream fs(filepath);
CSVRow row;
while (fs >> row)
{
vInstruments.push_back(Instrument(row[0], atoi(row[1].c_str()), atoi(row[2].c_str()), atoi(row[3].c_str())));
}

cout << "size: [" << vInstruments.size() << "]" << endl;
}

int main()
{
vector<Instrument> vInstruments(40000000);
TestRouteine("../InstradayWS/pen201502131002.txt", vInstruments);
system("PAUSE");
}

沒有留言:

張貼留言

2007 to 2023 HP and Dell Servers Comparison

  HP Gen5 to Gen11  using ChatGPT HP ProLiant Gen Active Years CPU Socket Popular HP CPUs Cores Base Clock Max RAM Capacity Comparable Dell ...