/docs/MyDocs

To get this branch, use:
bzr branch http://darksoft.org/webbzr/docs/MyDocs

« back to all changes in this revision

Viewing changes to Development/languages/C/Samples.CPP/bench/split_trim.cpp

  • Committer: Suren A. Chilingaryan
  • Date: 2009-04-09 03:21:08 UTC
  • Revision ID: csa@dside.dyndns.org-20090409032108-w4edamdh4adrgdu3
import

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#include <iostream>
 
2
#include <vector>
 
3
#include <string>
 
4
 
 
5
#include <boost/algorithm/string/classification.hpp>
 
6
#include <boost/algorithm/string/split.hpp>
 
7
#include <boost/algorithm/string/trim.hpp>
 
8
 
 
9
#include <boost/date_time/posix_time/ptime.hpp>
 
10
#include <boost/date_time/local_time/local_time.hpp>
 
11
 
 
12
#include <boost/lambda/lambda.hpp>
 
13
#include <boost/lambda/bind.hpp>
 
14
 
 
15
#include "string.hpp"
 
16
#include "locale.hpp"
 
17
 
 
18
using namespace std;
 
19
using namespace ds;
 
20
 
 
21
using namespace boost;
 
22
using namespace boost::lambda;
 
23
using namespace boost::posix_time;
 
24
using namespace boost::local_time;
 
25
 
 
26
typedef boost::split_iterator<string::iterator> string_split_iterator;
 
27
 
 
28
main(int argc, char *argv[]) {
 
29
    csv_string s;
 
30
    vector <csv_string> v;
 
31
    
 
32
    ptime start, stop;
 
33
 
 
34
    if (argc<2) {
 
35
        cout << argv[0] << " [file.csv]" << endl;
 
36
        abort();
 
37
    }
 
38
    
 
39
    
 
40
    std::ifstream f(argv[1]);
 
41
    if (!f) {
 
42
        cout << "Can't open input file " << argv[1] << endl;
 
43
        abort();
 
44
    }
 
45
 
 
46
        // configuring special locale
 
47
    std::locale loc(std::locale::classic(), new myctype<char>());
 
48
    
 
49
    while (std::getline(f, s, '\n'));   // caching
 
50
 
 
51
    f.clear();                  // forget we hit the end of file
 
52
    f.seekg(0, ios::beg);
 
53
 
 
54
    start = microsec_clock::local_time();
 
55
    while (std::getline(f, s, '\n')) {
 
56
        v = s.split(',');
 
57
//      cout << v[0] << endl;
 
58
    }
 
59
    stop = microsec_clock::local_time();
 
60
    time_period cdur0(start, stop);    
 
61
 
 
62
    f.clear();                  // forget we hit the end of file
 
63
    f.seekg(0, ios::beg);
 
64
    
 
65
    start = microsec_clock::local_time();
 
66
    while (std::getline(f, s, '\n')) {
 
67
        v.clear();
 
68
        for (ds::split_iterator i = s.create_split_iterator(','); i!=ds::split_iterator(); ++i) {
 
69
            v.push_back(*i);
 
70
        }
 
71
//      cout << v[0] << endl;
 
72
    }
 
73
    stop = microsec_clock::local_time();
 
74
    time_period cdur1(start, stop);    
 
75
 
 
76
    f.clear();                  // forget we hit the end of file
 
77
    f.seekg(0, ios::beg);
 
78
 
 
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;
 
84
    }
 
85
    stop = microsec_clock::local_time();
 
86
    time_period cdur2(start, stop);    
 
87
 
 
88
    f.clear();                  // forget we hit the end of file
 
89
    f.seekg(0, ios::beg);
 
90
 
 
91
    start = microsec_clock::local_time();
 
92
    while (std::getline(f, s, '\n')) {
 
93
        v.clear();
 
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)));
 
96
        }       
 
97
//      cout << v[0] << endl;
 
98
    }
 
99
    stop = microsec_clock::local_time();
 
100
    time_period cdur3(start, stop);    
 
101
 
 
102
    f.clear();                  // forget we hit the end of file
 
103
    f.seekg(0, ios::beg);
 
104
 
 
105
    start = microsec_clock::local_time();
 
106
    while (std::getline(f, s, '\n')) {
 
107
        v.clear();
 
108
        for (string_split_iterator i = make_split_iterator(s, first_finder(",", is_equal())); i!=string_split_iterator(); ++i) {
 
109
            string stmp;
 
110
            trim_copy_if(back_inserter(stmp), *i, is_space());
 
111
            v.push_back(stmp);
 
112
        }       
 
113
//      cout << v[0] << endl;
 
114
    }
 
115
    stop = microsec_clock::local_time();
 
116
    time_period cdur4(start, stop);    
 
117
 
 
118
    f.clear();                  // forget we hit the end of file
 
119
    f.seekg(0, ios::beg);
 
120
    
 
121
    start = microsec_clock::local_time();
 
122
    while (std::getline(f, s, '\n')) {
 
123
        v.clear();
 
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));
 
130
        }       
 
131
//      cout << v[2] << "." << endl;
 
132
    }
 
133
    stop = microsec_clock::local_time();
 
134
    time_period cdur5(start, stop);    
 
135
 
 
136
    f.clear();                  // forget we hit the end of file
 
137
    f.seekg(0, ios::beg);
 
138
 
 
139
    start = microsec_clock::local_time();
 
140
    while (std::getline(f, s, '\n')) {
 
141
        string stmp;
 
142
        v.clear();
 
143
 
 
144
        std::istringstream ins (s);
 
145
        ins.imbue(loc);
 
146
        while (ins >> stmp) v.push_back(stmp);
 
147
 
 
148
//      cout << v[0] << endl;
 
149
    }
 
150
    stop = microsec_clock::local_time();
 
151
    time_period cdur6(start, stop);    
 
152
 
 
153
    f.clear();                  // forget we hit the end of file
 
154
    f.seekg(0, ios::beg);
 
155
 
 
156
    start = microsec_clock::local_time();
 
157
    while (std::getline(f, s, '\n')) {
 
158
        std::istringstream ins (s);
 
159
        ins.imbue(loc);
 
160
        istream_iterator<csv_string> start(ins);
 
161
        istream_iterator<csv_string> end;
 
162
        v = vector<csv_string>(start, end);
 
163
        
 
164
//      cout << v[0] << endl;
 
165
    }
 
166
    stop = microsec_clock::local_time();
 
167
    time_period cdur7(start, stop);    
 
168
 
 
169
 
 
170
    
 
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;
 
180
}