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::copy - optimised for trivial copy (cf std::copy) |
---|
13 | * |
---|
14 | */ |
---|
15 | |
---|
16 | #include <iostream> |
---|
17 | #include <typeinfo> |
---|
18 | #include <algorithm> |
---|
19 | #include <iterator> |
---|
20 | #include <memory> |
---|
21 | |
---|
22 | #include <boost/test/included/prg_exec_monitor.hpp> |
---|
23 | #include <boost/timer.hpp> |
---|
24 | #include <boost/type_traits.hpp> |
---|
25 | |
---|
26 | using std::cout; |
---|
27 | using std::endl; |
---|
28 | using std::cin; |
---|
29 | |
---|
30 | namespace opt{ |
---|
31 | |
---|
32 | // |
---|
33 | // opt::copy |
---|
34 | // same semantics as std::copy |
---|
35 | // calls memcpy where appropiate. |
---|
36 | // |
---|
37 | |
---|
38 | namespace detail{ |
---|
39 | |
---|
40 | template<typename I1, typename I2, bool b> |
---|
41 | I2 copy_imp(I1 first, I1 last, I2 out, const boost::integral_constant<bool, b>&) |
---|
42 | { |
---|
43 | while(first != last) |
---|
44 | { |
---|
45 | *out = *first; |
---|
46 | ++out; |
---|
47 | ++first; |
---|
48 | } |
---|
49 | return out; |
---|
50 | } |
---|
51 | |
---|
52 | template<typename T> |
---|
53 | T* copy_imp(const T* first, const T* last, T* out, const boost::true_type&) |
---|
54 | { |
---|
55 | memcpy(out, first, (last-first)*sizeof(T)); |
---|
56 | return out+(last-first); |
---|
57 | } |
---|
58 | |
---|
59 | |
---|
60 | } |
---|
61 | |
---|
62 | template<typename I1, typename I2> |
---|
63 | inline I2 copy(I1 first, I1 last, I2 out) |
---|
64 | { |
---|
65 | // |
---|
66 | // We can copy with memcpy if T has a trivial assignment operator, |
---|
67 | // and if the iterator arguments are actually pointers (this last |
---|
68 | // requirement we detect with overload resolution): |
---|
69 | // |
---|
70 | typedef typename std::iterator_traits<I1>::value_type value_type; |
---|
71 | return detail::copy_imp(first, last, out, boost::has_trivial_assign<value_type>()); |
---|
72 | } |
---|
73 | |
---|
74 | }; // namespace opt |
---|
75 | |
---|
76 | // |
---|
77 | // define some global data: |
---|
78 | // |
---|
79 | const int array_size = 1000; |
---|
80 | int i_array_[array_size] = {0,}; |
---|
81 | const int ci_array_[array_size] = {0,}; |
---|
82 | char c_array_[array_size] = {0,}; |
---|
83 | const char cc_array_[array_size] = { 0,}; |
---|
84 | // |
---|
85 | // since arrays aren't iterators we define a set of pointer |
---|
86 | // aliases into the arrays (otherwise the compiler is entitled |
---|
87 | // to deduce the type passed to the template functions as |
---|
88 | // T (&)[N] rather than T*). |
---|
89 | int* i_array = i_array_; |
---|
90 | const int* ci_array = ci_array_; |
---|
91 | char* c_array = c_array_; |
---|
92 | const char* cc_array = cc_array_; |
---|
93 | |
---|
94 | const int iter_count = 1000000; |
---|
95 | |
---|
96 | int cpp_main(int argc, char* argv[]) |
---|
97 | { |
---|
98 | boost::timer t; |
---|
99 | double result; |
---|
100 | int i; |
---|
101 | cout << "Measuring times in micro-seconds per 1000 elements processed" << endl << endl; |
---|
102 | cout << "testing copy...\n" |
---|
103 | "[Some standard library versions may already perform this optimisation.]" << endl; |
---|
104 | |
---|
105 | // cache load: |
---|
106 | opt::copy(ci_array, ci_array + array_size, i_array); |
---|
107 | |
---|
108 | // time optimised version: |
---|
109 | t.restart(); |
---|
110 | for(i = 0; i < iter_count; ++i) |
---|
111 | { |
---|
112 | opt::copy(ci_array, ci_array + array_size, i_array); |
---|
113 | } |
---|
114 | result = t.elapsed(); |
---|
115 | cout << "opt::copy<const int*, int*>: " << result << endl; |
---|
116 | |
---|
117 | // cache load: |
---|
118 | std::copy(ci_array, ci_array + array_size, i_array); |
---|
119 | |
---|
120 | // time standard version: |
---|
121 | t.restart(); |
---|
122 | for(i = 0; i < iter_count; ++i) |
---|
123 | { |
---|
124 | std::copy(ci_array, ci_array + array_size, i_array); |
---|
125 | } |
---|
126 | result = t.elapsed(); |
---|
127 | cout << "std::copy<const int*, int*>: " << result << endl; |
---|
128 | |
---|
129 | // cache load: |
---|
130 | opt::copy(cc_array, cc_array + array_size, c_array); |
---|
131 | |
---|
132 | // time optimised version: |
---|
133 | t.restart(); |
---|
134 | for(i = 0; i < iter_count; ++i) |
---|
135 | { |
---|
136 | opt::copy(cc_array, cc_array + array_size, c_array); |
---|
137 | } |
---|
138 | result = t.elapsed(); |
---|
139 | cout << "opt::copy<const char*, char*>: " << result << endl; |
---|
140 | |
---|
141 | // cache load: |
---|
142 | std::copy(cc_array, cc_array + array_size, c_array); |
---|
143 | |
---|
144 | // time standard version: |
---|
145 | t.restart(); |
---|
146 | for(i = 0; i < iter_count; ++i) |
---|
147 | { |
---|
148 | std::copy(cc_array, cc_array + array_size, c_array); |
---|
149 | } |
---|
150 | result = t.elapsed(); |
---|
151 | cout << "std::copy<const char*, char*>: " << result << endl; |
---|
152 | |
---|
153 | return 0; |
---|
154 | } |
---|
155 | |
---|
156 | |
---|
157 | |
---|
158 | |
---|
159 | |
---|
160 | |
---|
161 | |
---|
162 | |
---|