14#define TESTSIZE 100000000
17template<
typename _InputIterator,
typename _Tp,
typename _BinaryOperation>
18_Tp
fpaccumulate(_InputIterator __first, _InputIterator __last, _Tp __init,
19 _BinaryOperation __binary_op)
21 for (; __first != __last; ++__first)
22 __init = __binary_op(__init, *__first);
27template<
typename SR,
typename _InputIterator,
typename _Tp>
28_Tp
sraccumulate(_InputIterator __first, _InputIterator __last, _Tp __init)
30 for (; __first != __last; ++__first)
31 __init = SR::add(__init, *__first);
37template <
class D,
class B>
43 return static_cast <D*
> (this-> do_clone ());
48 return new D (
static_cast <const D&
> (*
this));
60 virtual B * do_clone ()
const = 0;
64template <
class T,
class DER>
70 cout <<
"Destructing base" << endl;
74 return static_cast<DER*
>(
this)->
Square();
78 cout <<
"Creating base with element " << obj << endl;
79 static_cast<DER*
>(
this)->CreateImpl(obj);
83 static_cast<DER*
>(
this)->
Print();
93 cout <<
"Destructing derived" << endl;
98 cout <<
"Creating derived with element " << obj << endl;
110 cout <<
"Element is " << ele << endl;
117template <
class T,
int N>
123 cout <<
"Constructing" << endl;
124 vec =
new vector<T>(N);
129 cout <<
"Copy constructing" << endl;
130 vec =
new vector<T>(*rhs.
vec);
134 cout <<
"Assigning" << endl;
140 vec =
new vector<T>(*rhs.
vec);
142 cout <<
"Assignment returning" << endl;
146 template <
typename SR>
150 for(
int i=0; i <
vec->size(); i++)
152 (*vec)[i] = SR::add((*rhs.
vec)[i], (*
vec)[i]);
161 EleAdd< PT > (duplicate);
166 cout <<
"Destructing object with first element " << (*vec)[0] << endl;
175 for(
int i=0; i<N; ++i)
176 cout << (*
vec)[i] <<
" ";
187 template <
typename T>
194 string const&
str()
const {
return m_value; }
240 cout <<
sizeof(tr1::tuple<int, int, double>) << endl;
242 int * arrint =
new int[5];
243 double * arrdouble =
new double[10];
244 float * arrfloat =
new float[5];
247 transform(arrfloat, arrfloat+5,arrfloat, bind1st(divides<float>(), 1));
248 for(
int i=0; i<5; ++i)
249 cout << arrfloat[i] <<
" ";
255 int *
A =
new int[5];
258 transform(
A,
A+5,
A, bind2nd(minus<int>(), 10));
259 for(
int i=0; i<5; ++i)
267 (*(BushJr.
vec))[0] = 100;
281 vector<int> v = *(BushJr.
vec);
282 cout << v.size() <<
" elements, occupying "<<
sizeof(v) <<
" bytes"<< endl;
294 T_promote result = a + b;
295 cout << result << endl;
297 vector<int>(3).swap(v);
298 cout <<
"size of v: " << v.size() <<
" , capacity of v: " << v.capacity() << endl;
300 cout <<
"******* vector performance test *******" << endl;
304 vector<int> inserted;
307 gettimeofday(&tim, NULL);
308 double t1=tim.tv_sec+(tim.tv_usec/1000000.0);
310 std::copy(source, source+
TESTSIZE, destination.begin());
312 gettimeofday(&tim, NULL);
313 double t2=tim.tv_sec+(tim.tv_usec/1000000.0);
314 printf(
"%.6lf seconds elapsed for std::copy\n", t2-t1);
317 gettimeofday(&tim, NULL);
318 t1=tim.tv_sec+(tim.tv_usec/1000000.0);
320 inserted.insert(inserted.begin(), source, source+
TESTSIZE);
322 gettimeofday(&tim, NULL);
323 t2=tim.tv_sec+(tim.tv_usec/1000000.0);
324 printf(
"%.6lf seconds elapsed with insert\n", t2-t1);
327 gettimeofday(&tim, NULL);
328 t1=tim.tv_sec+(tim.tv_usec/1000000.0);
330 memcpy(&destination[0], source,
TESTSIZE*
sizeof(
int));
332 gettimeofday(&tim, NULL);
333 t2=tim.tv_sec+(tim.tv_usec/1000000.0);
334 printf(
"%.6lf seconds elapsed with memcpy\n", t2-t1);
337 gettimeofday(&tim, NULL);
338 t1=tim.tv_sec+(tim.tv_usec/1000000.0);
341 destination[i] = source[i];
343 gettimeofday(&tim, NULL);
344 t2=tim.tv_sec+(tim.tv_usec/1000000.0);
345 printf(
"%.6lf seconds elapsed with loops\n", t2-t1);
349 cout <<
"******* accumulate performance test *******" << endl;
351 gettimeofday(&tim, NULL);
352 t1=tim.tv_sec+(tim.tv_usec/1000000.0);
354 int acc =
fpaccumulate(destination.begin(), destination.end(), 0, plus<int>());
356 gettimeofday(&tim, NULL);
357 t2=tim.tv_sec+(tim.tv_usec/1000000.0);
358 printf(
"%.6lf seconds elapsed with plus<int> function object, gave result: %d\n", t2-t1, acc);
360 gettimeofday(&tim, NULL);
361 t1=tim.tv_sec+(tim.tv_usec/1000000.0);
364 acc = sraccumulate< PT > (destination.begin(), destination.end(), 0);
366 gettimeofday(&tim, NULL);
367 t2=tim.tv_sec+(tim.tv_usec/1000000.0);
368 printf(
"%.6lf seconds elapsed with SR::add static method, gave result: %d\n", t2-t1, acc);
371 gettimeofday(&tim, NULL);
372 t1=tim.tv_sec+(tim.tv_usec/1000000.0);
374 acc =
fpaccumulate(destination.begin(), destination.end(), 0, &add_func<int>);
376 gettimeofday(&tim, NULL);
377 t2=tim.tv_sec+(tim.tv_usec/1000000.0);
378 printf(
"%.6lf seconds elapsed with function pointer, gave result: %d\n", t2-t1, acc);
381 cout <<
"******* array of structs test *******" << endl;
387 random_shuffle(random, random+
TESTSIZE);
395 arrayofstrs[i].
first = 1;
396 arrayofstrs[i].
second = 2;
397 arrayofstrs[i].
third = 3;
399 strofarrays.
firsts[i] = 1;
401 strofarrays.
thirds[i] = 3;
404 gettimeofday(&tim, NULL);
405 t1=tim.tv_sec+(tim.tv_usec/1000000.0);
408 arrayofstrs[linear[i]].
first += 1;
409 arrayofstrs[linear[i]].
second += 2;
410 arrayofstrs[linear[i]].
third += 3;
412 gettimeofday(&tim, NULL);
413 t2=tim.tv_sec+(tim.tv_usec/1000000.0);
414 printf(
"%.6lf seconds elapsed for streaming array of structs\n", t2-t1);
416 gettimeofday(&tim, NULL);
417 t1=tim.tv_sec+(tim.tv_usec/1000000.0);
420 strofarrays.
firsts[linear[i]] += 1;
421 strofarrays.
seconds[linear[i]] += 2;
422 strofarrays.
thirds[linear[i]] += 3;
424 gettimeofday(&tim, NULL);
425 t2=tim.tv_sec+(tim.tv_usec/1000000.0);
426 printf(
"%.6lf seconds elapsed for streaming struct of arrays\n", t2-t1);
428 gettimeofday(&tim, NULL);
429 t1=tim.tv_sec+(tim.tv_usec/1000000.0);
432 arrayofstrs[random[i]].
first += 1;
433 arrayofstrs[random[i]].
second += 2;
434 arrayofstrs[random[i]].
third += 3;
436 gettimeofday(&tim, NULL);
437 t2=tim.tv_sec+(tim.tv_usec/1000000.0);
438 printf(
"%.6lf seconds elapsed for randomly accessing array of structs\n", t2-t1);
440 gettimeofday(&tim, NULL);
441 t1=tim.tv_sec+(tim.tv_usec/1000000.0);
444 strofarrays.
firsts[random[i]] += 1;
445 strofarrays.
seconds[random[i]] += 2;
446 strofarrays.
thirds[random[i]] += 3;
448 gettimeofday(&tim, NULL);
449 t2=tim.tv_sec+(tim.tv_usec/1000000.0);
450 printf(
"%.6lf seconds elapsed for randomly accessing struct of arrays\n", t2-t1);
AClass & operator=(T val)
string const & str() const
Base< T, Derived< T > > Square()
Dummy(const Dummy< T, N > &rhs)
Dummy< T, N > & operator=(const Dummy< T, N > &rhs)
void EleAdd(const Dummy< T, N > &rhs)
void iota(_ForwardIter __first, _ForwardIter __last, T __value)
_Tp sraccumulate(_InputIterator __first, _InputIterator __last, _Tp __init)
ostream & operator<<(ostream &os, AClass const &obj)
T add_func(const T &a, const T &b)
_Tp fpaccumulate(_InputIterator __first, _InputIterator __last, _Tp __init, _BinaryOperation __binary_op)