1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
|
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
#include <vector>
#include <chrono>
#include <cstdio>
using namespace std;
char buf[1024*1024*10];
struct Trades {
unsigned short int month, day, year, hour, min, seconds;
double open, high, low, close;
unsigned int volume;
string name;
string date;
string time;
string operation;
};
void createData1 (ifstream &dataFile, vector<Trades> &matrix)
{
if (dataFile.is_open())
{
string line;
int linecount = 0;
// Minimum time it takes to just read the file
while ( getline(dataFile,line) ) {
++linecount;
}
cout << linecount << endl;
}
}
void createDataC()
{
unsigned short int month, day, year, hour, min, seconds;
double open, high, low, close;
unsigned int volume;
int linecount = 0;
FILE *fp = fopen("5m_records.csv", "r");
while (fscanf(fp, "%hu%*c%hu%*c%hu%*c%hu%*c%hu%*c%hu%*c%*c%*c%lf%*c%lf%*c%lf%*c%lf%*c%u",
&month, &day, &year, &hour, &min, &seconds,
&open, &high, &low, &close,
&volume) == 11) {
++linecount;
}
cout << linecount << '\n';;
}
void createData2 (ifstream &dataFile, vector<Trades> &matrix)
{
unsigned short int month, day, year, hour, min, seconds;
double open, high, low, close;
unsigned int volume;
char delim;
if (dataFile.is_open())
{
int linecount = 0;
// delta-time it takes to parse the input
while (dataFile >> month >> delim >> day >> delim >> year >> hour >> delim >> min >> delim >> seconds >>
delim >> delim >> delim >> open >> delim >> high >>
delim >> low >> delim >> close >> delim >> volume)
{
++linecount;
}
cout << linecount << endl;
}
}
void createData3 (ifstream &dataFile, vector<Trades> &matrix)
{
unsigned short int month, day, year, hour, min, seconds;
double open, high, low, close;
unsigned int volume;
char delim;
if (dataFile.is_open())
{
while (dataFile >> month >> delim >> day >> delim >> year >> hour >> delim >> min >> delim >> seconds >>
delim >> delim >> delim >> open >> delim >> high >>
delim >> low >> delim >> close >> delim >> volume)
{
matrix.push_back({month, day, year, hour, min, seconds, open, high, low, close, volume});
}
cout << matrix.size() << endl;
}
}
int main()
{
using namespace std::chrono;
ifstream fin;
fin.rdbuf()->pubsetbuf(buf, sizeof(buf));
fin.open("5m_records.csv");
{
vector<Trades> matrix;
fin.seekg(0);
auto t0 = steady_clock::now();
createData1(fin,matrix);
auto t1 = steady_clock::now();
auto d = duration_cast<duration<double>>(t1-t0);
cout << d.count() << " seconds" << endl;
}
{
vector<Trades> matrix;
cout << "Reading with C library\n";
auto t0 = steady_clock::now();
createDataC();
auto t1 = steady_clock::now();
auto d = duration_cast<duration<double>>(t1-t0);
cout << d.count() << " seconds" << endl;
}
{
vector<Trades> matrix;
auto t0 = steady_clock::now();
fin.clear();
fin.seekg(0);
createData2(fin,matrix);
auto t1 = steady_clock::now();
auto d = duration_cast<duration<double>>(t1-t0);
cout << d.count() << " seconds" << endl;
}
{
vector<Trades> matrix;
fin.clear();
fin.seekg(0);
auto t0 = steady_clock::now();
createData3(fin,matrix);
auto t1 = steady_clock::now();
auto d = duration_cast<duration<double>>(t1-t0);
cout << d.count() << " seconds" << endl;
}
}
|