5
#include <boost/algorithm/string/classification.hpp>
6
#include <boost/algorithm/string/split.hpp>
7
#include <boost/algorithm/string/trim.hpp>
9
#include <boost/date_time/posix_time/ptime.hpp>
10
#include <boost/date_time/local_time/local_time.hpp>
12
#include <boost/lambda/lambda.hpp>
13
#include <boost/lambda/bind.hpp>
21
using namespace boost;
22
using namespace boost::lambda;
23
using namespace boost::posix_time;
24
using namespace boost::local_time;
26
typedef boost::split_iterator<string::iterator> string_split_iterator;
28
main(int argc, char *argv[]) {
30
vector <csv_string> v;
35
cout << argv[0] << " [file.csv]" << endl;
40
std::ifstream f(argv[1]);
42
cout << "Can't open input file " << argv[1] << endl;
46
// configuring special locale
47
std::locale loc(std::locale::classic(), new myctype<char>());
49
while (std::getline(f, s, '\n')); // caching
51
f.clear(); // forget we hit the end of file
54
start = microsec_clock::local_time();
55
while (std::getline(f, s, '\n')) {
57
// cout << v[0] << endl;
59
stop = microsec_clock::local_time();
60
time_period cdur0(start, stop);
62
f.clear(); // forget we hit the end of file
65
start = microsec_clock::local_time();
66
while (std::getline(f, s, '\n')) {
68
for (ds::split_iterator i = s.create_split_iterator(','); i!=ds::split_iterator(); ++i) {
71
// cout << v[0] << endl;
73
stop = microsec_clock::local_time();
74
time_period cdur1(start, stop);
76
f.clear(); // forget we hit the end of file
79
start = microsec_clock::local_time();
80
while (std::getline(f, s, '\n')) {
81
boost::split(v, s, boost::is_any_of(","));
82
for_each(v.begin(), v.end(), bind(trim<string>, _1, std::locale()));
83
// cout << v[0] << endl;
85
stop = microsec_clock::local_time();
86
time_period cdur2(start, stop);
88
f.clear(); // forget we hit the end of file
91
start = microsec_clock::local_time();
92
while (std::getline(f, s, '\n')) {
94
for (string_split_iterator i = make_split_iterator(s, first_finder(",", is_equal())); i!=string_split_iterator(); ++i) {
95
v.push_back(trim_copy(copy_range<std::string>(*i)));
97
// cout << v[0] << endl;
99
stop = microsec_clock::local_time();
100
time_period cdur3(start, stop);
102
f.clear(); // forget we hit the end of file
103
f.seekg(0, ios::beg);
105
start = microsec_clock::local_time();
106
while (std::getline(f, s, '\n')) {
108
for (string_split_iterator i = make_split_iterator(s, first_finder(",", is_equal())); i!=string_split_iterator(); ++i) {
110
trim_copy_if(back_inserter(stmp), *i, is_space());
113
// cout << v[0] << endl;
115
stop = microsec_clock::local_time();
116
time_period cdur4(start, stop);
118
f.clear(); // forget we hit the end of file
119
f.seekg(0, ios::beg);
121
start = microsec_clock::local_time();
122
while (std::getline(f, s, '\n')) {
124
for (string_split_iterator i = make_split_iterator(s, first_finder(",", is_equal())); i!=string_split_iterator(); ++i) {
125
string::iterator tend =
126
boost::algorithm::detail::trim_end(begin(*i), end(*i), is_space());
127
string::iterator tbegin =
128
boost::algorithm::detail::trim_begin(begin(*i), tend, is_space());
129
v.push_back(string(tbegin, tend));
131
// cout << v[2] << "." << endl;
133
stop = microsec_clock::local_time();
134
time_period cdur5(start, stop);
136
f.clear(); // forget we hit the end of file
137
f.seekg(0, ios::beg);
139
start = microsec_clock::local_time();
140
while (std::getline(f, s, '\n')) {
144
std::istringstream ins (s);
146
while (ins >> stmp) v.push_back(stmp);
148
// cout << v[0] << endl;
150
stop = microsec_clock::local_time();
151
time_period cdur6(start, stop);
153
f.clear(); // forget we hit the end of file
154
f.seekg(0, ios::beg);
156
start = microsec_clock::local_time();
157
while (std::getline(f, s, '\n')) {
158
std::istringstream ins (s);
160
istream_iterator<csv_string> start(ins);
161
istream_iterator<csv_string> end;
162
v = vector<csv_string>(start, end);
164
// cout << v[0] << endl;
166
stop = microsec_clock::local_time();
167
time_period cdur7(start, stop);
171
cout << "Own algorithm : " << cdur0.length() << endl;
172
cout << "Own algorithm (iter) : " << cdur1.length() << endl;
173
cout << "Boost stright forward : " << cdur2.length() << endl;
174
cout << "Boost optimized (1) : " << cdur3.length() << endl;
175
cout << "Boost optimized (2) : " << cdur4.length() << endl;
176
cout << "Boost optimized (3) : " << cdur5.length() << endl;
177
cout << "Overriding ctype facet (1) : " << cdur6.length() << " (*)" << endl;
178
cout << "Overriding ctype facet (2) : " << cdur7.length() << " (*)" << endl;
179
cout << endl << "(*) can read directly into the numericals, but triming should be worked out" << endl;