Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: downloads/boost_1_34_1/libs/type_traits/examples/fill_example.cpp @ 29

Last change on this file since 29 was 29, checked in by landauf, 16 years ago

updated boost from 1_33_1 to 1_34_1

File size: 3.8 KB
Line 
1
2/*
3 *
4 * (C) Copyright John Maddock 1999-2005.
5 * Use, modification and distribution are subject to the
6 * Boost Software License, Version 1.0. (See accompanying file
7 * LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
8 *
9 * This file provides some example of type_traits usage -
10 * by "optimising" various algorithms:
11 *
12 * opt::fill - optimised for trivial copy/small types (cf std::fill)
13 *
14 */
15
16#include <iostream>
17#include <typeinfo>
18#include <algorithm>
19#include <iterator>
20#include <memory>
21#include <cstring>
22
23#include <boost/test/included/prg_exec_monitor.hpp>
24#include <boost/timer.hpp>
25#include <boost/type_traits.hpp>
26
27#if defined(BOOST_NO_STDC_NAMESPACE) || (defined(std) && defined(__SGI_STL_PORT))
28namespace std{ using :: memset; }
29#endif
30
31using std::cout;
32using std::endl;
33using std::cin;
34
35namespace opt{
36//
37// fill
38// same as std::fill, but uses memset where appropriate
39//
40namespace detail{
41
42template <typename I, typename T, bool b>
43void do_fill(I first, I last, const T& val, const boost::integral_constant<bool, b>&)
44{
45   while(first != last)
46   {
47      *first = val;
48      ++first;
49   }
50}
51
52template <typename T>
53void do_fill(T* first, T* last, const T& val, const boost::true_type&)
54{
55   std::memset(first, val, last-first);
56}
57
58}
59
60template <class I, class T>
61inline void fill(I first, I last, const T& val)
62{
63   //
64   // We can do an optimised fill if T has a trivial assignment
65   // operator and if it's size is one:
66   //
67   typedef boost::integral_constant<bool, 
68      ::boost::has_trivial_assign<T>::value && (sizeof(T) == 1)> truth_type;
69   detail::do_fill(first, last, val, truth_type());
70}
71
72};   // namespace opt
73
74//
75// define some global data:
76//
77const int array_size = 1000;
78int i_array_[array_size] = {0,};
79const int ci_array_[array_size] = {0,};
80char c_array_[array_size] = {0,};
81const char cc_array_[array_size] = { 0,};
82//
83// since arrays aren't iterators we define a set of pointer
84// aliases into the arrays (otherwise the compiler is entitled
85// to deduce the type passed to the template functions as
86// T (&)[N] rather than T*).
87int* i_array = i_array_;
88const int* ci_array = ci_array_;
89char* c_array = c_array_;
90const char* cc_array = cc_array_;
91
92const int iter_count = 1000000;
93
94int cpp_main(int argc, char* argv[])
95{
96   boost::timer t;
97   double result;
98   int i;
99   //
100   // test destroy_array,
101   // compare destruction time of an array of ints
102   // with unoptimised form.
103   //
104   cout << "Measuring times in micro-seconds per 1000 elements processed" << endl << endl;
105
106   cout << "testing fill(char)...\n"
107   "[Some standard library versions may already perform this optimisation.]" << endl;
108
109   // cache load:
110   opt::fill(c_array, c_array + array_size, (char)3);
111
112   // time optimised version:
113   t.restart();
114   for(i = 0; i < iter_count; ++i)
115   {
116      opt::fill(c_array, c_array + array_size, (char)3);
117   }
118   result = t.elapsed();
119   cout << "opt::fill<char*, char>: " << result << endl;
120
121   // cache load:
122   std::fill(c_array, c_array + array_size, (char)3);
123
124   // time standard version:
125   t.restart();
126   for(i = 0; i < iter_count; ++i)
127   {
128      std::fill(c_array, c_array + array_size, (char)3);
129   }
130   result = t.elapsed();
131   cout << "std::fill<char*, char>: " << result << endl << endl;
132
133   cout << "testing fill(int)...\n" << endl;
134
135   // cache load:
136   opt::fill(i_array, i_array + array_size, 3);
137
138   // timer optimised version:
139   t.restart();
140   for(i = 0; i < iter_count; ++i)
141   {
142      opt::fill(i_array, i_array + array_size, 3);
143   }
144   result = t.elapsed();
145   cout << "opt::fill<int*, int>: " << result << endl;
146
147   // cache load:
148   std::fill(i_array, i_array + array_size, 3);
149
150   // time standard version:
151   t.restart();
152   for(i = 0; i < iter_count; ++i)
153   {
154      std::fill(i_array, i_array + array_size, 3);
155   }
156   result = t.elapsed();
157   cout << "std::fill<int*, int>: " << result << endl << endl;
158
159   return 0;
160}
161
162
163
164
165
166
Note: See TracBrowser for help on using the repository browser.