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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
|
/* Boost.Flyweight example of performance comparison.
*
* Copyright 2006-2008 Joaquin M Lopez Munoz.
* Distributed under the Boost Software License, Version 1.0.
* (See accompanying file LICENSE_1_0.txt or copy at
* http://www.boost.org/LICENSE_1_0.txt)
*
* See http://www.boost.org/libs/flyweight for library home page.
*/
#include <boost/flyweight/flyweight.hpp>
#include <boost/flyweight/hashed_factory.hpp>
#include <boost/flyweight/set_factory.hpp>
#include <boost/flyweight/static_holder.hpp>
#include <boost/flyweight/simple_locking.hpp>
#include <boost/flyweight/refcounted.hpp>
#include <boost/flyweight/no_tracking.hpp>
#include <boost/mpl/bool.hpp>
#include <boost/tokenizer.hpp>
#include <algorithm>
#include <cstddef>
#include <cstdlib>
#include <ctime>
#include <fstream>
#include <iostream>
#include <numeric>
#include <sstream>
#include <string>
#include <vector>
#if defined(BOOST_NO_STDC_NAMESPACE)
namespace std{
using ::clock;
using ::clock_t;
using ::exit;
}
#endif
using namespace boost::flyweights;
/* Instrumented allocator family keeping track of the memory in
* current use.
*/
std::size_t count_allocator_mem=0;
template<typename T>
class count_allocator
{
public:
typedef std::size_t size_type;
typedef std::ptrdiff_t difference_type;
typedef T* pointer;
typedef const T* const_pointer;
typedef T& reference;
typedef const T& const_reference;
typedef T value_type;
template<class U>struct rebind{typedef count_allocator<U> other;};
count_allocator(){}
count_allocator(const count_allocator<T>&){}
template<class U>count_allocator(const count_allocator<U>&,int=0){}
pointer address(reference x)const{return &x;}
const_pointer address(const_reference x)const{return &x;}
pointer allocate(size_type n,const void* =0)
{
pointer p=(T*)(new char[n*sizeof(T)]);
count_allocator_mem+=n*sizeof(T);
return p;
}
void deallocate(void* p,size_type n)
{
count_allocator_mem-=n*sizeof(T);
delete [](char *)p;
}
size_type max_size() const{return (size_type )(-1);}
void construct(pointer p,const T& val){new(p)T(val);}
void destroy(pointer p){p->~T();}
friend bool operator==(const count_allocator&,const count_allocator&)
{
return true;
}
friend bool operator!=(const count_allocator&,const count_allocator&)
{
return false;
}
};
template<>
class count_allocator<void>
{
public:
typedef void* pointer;
typedef const void* const_pointer;
typedef void value_type;
template<class U>struct rebind{typedef count_allocator<U> other;};
};
/* Define some count_allocator-based types and Boost.Flyweight components */
typedef std::basic_string<
char,std::char_traits<char>,count_allocator<char>
> count_string;
typedef hashed_factory<
boost::hash<boost::mpl::_2>,
std::equal_to<boost::mpl::_2>,
count_allocator<boost::mpl::_1>
> count_hashed_factory;
typedef set_factory<
std::less<boost::mpl::_2>,
count_allocator<boost::mpl::_1>
> count_set_factory;
/* Some additional utilities used by the test routine */
class timer
{
public:
timer(){restart();}
void restart(){t=std::clock();}
void time(const char* str)
{
std::cout<<str<<": "<<(double)(std::clock()-t)/CLOCKS_PER_SEC<<" s\n";
}
private:
std::clock_t t;
};
template<typename T>
struct is_flyweight:
boost::mpl::false_{};
template<
typename T,
typename Arg1,typename Arg2,typename Arg3,typename Arg4,typename Arg5
>
struct is_flyweight<flyweight<T,Arg1,Arg2,Arg3,Arg4,Arg5> >:
boost::mpl::true_{};
struct length_adder
{
std::size_t operator()(std::size_t n,const count_string& x)const
{
return n+x.size();
}
};
/* Measure time and memory performance for a String, which is assumed
* to be either a plain string type or a string flyweight.
*/
template<typename String>
struct test
{
static std::size_t run(const std::string& file)
{
typedef std::vector<String,count_allocator<String> > count_vector;
/* Define a tokenizer on std::istreambuf. */
typedef std::istreambuf_iterator<char> char_iterator;
typedef boost::tokenizer<
boost::char_separator<char>,
char_iterator
> tokenizer;
std::ifstream ifs(file.c_str());
if(!ifs){
std::cout<<"can't open "<<file<<std::endl;
std::exit(EXIT_FAILURE);
}
/* Initialization; tokenize using space and common punctuaction as
* separators, and keeping the separators.
*/
timer t;
tokenizer tok=tokenizer(
char_iterator(ifs),char_iterator(),
boost::char_separator<char>(
"",
"\t\n\r !\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~"));
count_vector txt;
for(tokenizer::iterator it=tok.begin();it!=tok.end();++it){
txt.push_back(String(it->c_str()));
}
t.time("initialization time");
/* Assignment */
t.restart();
count_vector txt2;
for(int i=0;i<10;++i){
txt2.insert(txt2.end(),txt.begin(),txt.end());
}
t.time("assignment time");
/* Equality comparison */
t.restart();
std::size_t c=0;
for(int i=0;i<100;++i){
c+=std::count(txt.begin(),txt.end(),txt[c%txt.size()]);
}
t.time("equality comparison time");
/* Value access */
t.restart();
std::size_t s=0;
for(int i=0;i<20;++i){
s+=std::accumulate(txt2.begin(),txt2.end(),s,length_adder());
}
t.time("value access time");
std::cout<<"bytes used: "<<count_allocator_mem;
if(is_flyweight<String>::value){
std::size_t flyweight_mem=(txt.capacity()+txt2.capacity())*sizeof(String);
std::cout<<"= flyweights("<<flyweight_mem
<<")+values("<<count_allocator_mem-flyweight_mem<<")";
}
std::cout<<"\n";
return c+s;
}
};
/* table of test cases for the user to select from */
struct test_case
{
const char* name;
std::size_t (*test)(const std::string&);
};
test_case test_table[]=
{
{
"simple string",
test<count_string>::run
},
{
"flyweight, hashed factory",
test<flyweight<count_string,count_hashed_factory> >::run
},
{
"flyweight, hashed factory, no tracking",
test<flyweight<count_string,count_hashed_factory,no_tracking> >::run
},
{
"flyweight, set-based factory",
test<flyweight<count_string,count_set_factory> >::run
},
{
"flyweight, set-based factory, no tracking",
test<flyweight<count_string,count_set_factory,no_tracking> >::run
}
};
const int num_test_cases=sizeof(test_table)/sizeof(test_case);
int main()
{
try{
for(int i=0;i<num_test_cases;++i){
std::cout<<i+1<<". "<<test_table[i].name<<"\n";
}
int option=-1;
for(;;){
std::cout<<"select option, enter to exit: ";
std::string str;
std::getline(std::cin,str);
if(str.empty())std::exit(EXIT_SUCCESS);
std::istringstream istr(str);
istr>>option;
if(option>=1&&option<=num_test_cases){
--option; /* pass from 1-based menu to 0-based test_table */
break;
}
}
std::cout<<"enter file name: ";
std::string file;
std::getline(std::cin,file);
std::size_t result=0;
result=test_table[option].test(file);
}
catch(const std::exception& e){
std::cout<<"error: "<<e.what()<<"\n";
}
return 0;
}
|