Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: downloads/boost_1_34_1/libs/numeric/ublas/bench1/bench11.cpp @ 29

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

updated boost from 1_33_1 to 1_34_1

File size: 8.5 KB
Line 
1//
2//  Copyright (c) 2000-2002
3//  Joerg Walter, Mathias Koch
4//
5//  Permission to use, copy, modify, distribute and sell this software
6//  and its documentation for any purpose is hereby granted without fee,
7//  provided that the above copyright notice appear in all copies and
8//  that both that copyright notice and this permission notice appear
9//  in supporting documentation.  The authors make no representations
10//  about the suitability of this software for any purpose.
11//  It is provided "as is" without express or implied warranty.
12//
13//  The authors gratefully acknowledge the support of
14//  GeNeSys mbH & Co. KG in producing this work.
15//
16
17#include "bench1.hpp"
18
19template<class T, int N>
20struct bench_c_inner_prod {
21    typedef T value_type;
22
23    void operator () (int runs) const {
24        try {
25            static typename c_vector_traits<T, N>::type v1, v2;
26            initialize_c_vector<T, N> () (v1);
27            initialize_c_vector<T, N> () (v2);
28            boost::timer t;
29            for (int i = 0; i < runs; ++ i) {
30                static value_type s (0);
31                for (int j = 0; j < N; ++ j) {
32                    s += v1 [j] * v2 [j];
33                }
34//                sink_scalar (s);
35            }
36            footer<value_type> () (N, N - 1, runs, t.elapsed ());
37        }
38        catch (std::exception &e) {
39            std::cout << e.what () << std::endl;
40        }
41    }
42};
43template<class V, int N>
44struct bench_my_inner_prod {
45    typedef typename V::value_type value_type;
46
47    void operator () (int runs) const {
48        try {
49            static V v1 (N), v2 (N);
50            initialize_vector (v1);
51            initialize_vector (v2);
52            boost::timer t;
53            for (int i = 0; i < runs; ++ i) {
54                static value_type s (0);
55                s = ublas::inner_prod (v1, v2);
56//                sink_scalar (s);
57            }
58            footer<value_type> () (N, N - 1, runs, t.elapsed ());
59        }
60        catch (std::exception &e) {
61            std::cout << e.what () << std::endl;
62        }
63    }
64};
65template<class V, int N>
66struct bench_cpp_inner_prod {
67    typedef typename V::value_type value_type;
68
69    void operator () (int runs) const {
70        try {
71            static V v1 (N), v2 (N);
72            initialize_vector (v1);
73            initialize_vector (v2);
74            boost::timer t;
75            for (int i = 0; i < runs; ++ i) {
76                static value_type s (0);
77                s = (v1 * v2).sum ();
78//                sink_scalar (s);
79            }
80            footer<value_type> () (N, N - 1, runs, t.elapsed ());
81        }
82        catch (std::exception &e) {
83            std::cout << e.what () << std::endl;
84        }
85    }
86};
87
88template<class T, int N>
89struct bench_c_vector_add {
90    typedef T value_type;
91
92    void operator () (int runs) const {
93        try {
94            static typename c_vector_traits<T, N>::type v1, v2, v3;
95            initialize_c_vector<T, N> () (v1);
96            initialize_c_vector<T, N> () (v2);
97            boost::timer t;
98            for (int i = 0; i < runs; ++ i) {
99                for (int j = 0; j < N; ++ j) {
100                    v3 [j] = - (v1 [j] + v2 [j]);
101                }
102//                sink_c_vector<T, N> () (v3);
103            }
104            footer<value_type> () (0, 2 * N, runs, t.elapsed ());
105        }
106        catch (std::exception &e) {
107            std::cout << e.what () << std::endl;
108        }
109    }
110};
111template<class V, int N>
112struct bench_my_vector_add {
113    typedef typename V::value_type value_type;
114
115    void operator () (int runs, safe_tag) const {
116        try {
117            static V v1 (N), v2 (N), v3 (N);
118            initialize_vector (v1);
119            initialize_vector (v2);
120            boost::timer t;
121            for (int i = 0; i < runs; ++ i) {
122                v3 = - (v1 + v2);
123//                sink_vector (v3);
124            }
125            footer<value_type> () (0, 2 * N, runs, t.elapsed ());
126        }
127        catch (std::exception &e) {
128            std::cout << e.what () << std::endl;
129        }
130    }
131    void operator () (int runs, fast_tag) const {
132        try {
133            static V v1 (N), v2 (N), v3 (N);
134            initialize_vector (v1);
135            initialize_vector (v2);
136            boost::timer t;
137            for (int i = 0; i < runs; ++ i) {
138                v3.assign (- (v1 + v2));
139//                sink_vector (v3);
140            }
141            footer<value_type> () (0, 2 * N, runs, t.elapsed ());
142        }
143        catch (std::exception &e) {
144            std::cout << e.what () << std::endl;
145        }
146    }
147};
148template<class V, int N>
149struct bench_cpp_vector_add {
150    typedef typename V::value_type value_type;
151
152    void operator () (int runs) const {
153        try {
154            static V v1 (N), v2 (N), v3 (N);
155            initialize_vector (v1);
156            initialize_vector (v2);
157            boost::timer t;
158            for (int i = 0; i < runs; ++ i) {
159                v3 = - (v1 + v2);
160//                sink_vector (v3);
161            }
162            footer<value_type> () (0, 2 * N, runs, t.elapsed ());
163        }
164        catch (std::exception &e) {
165            std::cout << e.what () << std::endl;
166        }
167    }
168};
169
170// Benchmark O (n)
171template<class T, int N>
172void bench_1<T, N>::operator () (int runs) {
173    header ("bench_1");
174
175    header ("inner_prod");
176
177    header ("C array");
178    bench_c_inner_prod<T, N> () (runs);
179
180#ifdef USE_C_ARRAY
181    header ("c_vector");
182    bench_my_inner_prod<ublas::c_vector<T, N>, N> () (runs);
183#endif
184
185#ifdef USE_BOUNDED_ARRAY
186    header ("vector<bounded_array>");
187    bench_my_inner_prod<ublas::vector<T, ublas::bounded_array<T, N> >, N> () (runs);
188#endif
189
190#ifdef USE_UNBOUNDED_ARRAY
191    header ("vector<unbounded_array>");
192    bench_my_inner_prod<ublas::vector<T, ublas::unbounded_array<T> >, N> () (runs);
193#endif
194
195#ifdef USE_STD_VALARRAY
196    header ("vector<std::valarray>");
197    bench_my_inner_prod<ublas::vector<T, std::valarray<T> >, N> () ();
198#endif
199
200#ifdef USE_STD_VECTOR
201    header ("vector<std::vector>");
202    bench_my_inner_prod<ublas::vector<T, std::vector<T> >, N> () (runs);
203#endif
204
205#ifdef USE_STD_VALARRAY
206    header ("std::valarray");
207    bench_cpp_inner_prod<std::valarray<T>, N> () (runs);
208#endif
209
210    header ("vector + vector");
211
212    header ("C array");
213    bench_c_vector_add<T, N> () (runs);
214
215#ifdef USE_C_ARRAY
216    header ("c_vector safe");
217    bench_my_vector_add<ublas::c_vector<T, N>, N> () (runs, safe_tag ());
218
219    header ("c_vector fast");
220    bench_my_vector_add<ublas::c_vector<T, N>, N> () (runs, fast_tag ());
221#endif
222
223#ifdef USE_BOUNDED_ARRAY
224    header ("vector<bounded_array> safe");
225    bench_my_vector_add<ublas::vector<T, ublas::bounded_array<T, N> >, N> () (runs, safe_tag ());
226
227    header ("vector<bounded_array> fast");
228    bench_my_vector_add<ublas::vector<T, ublas::bounded_array<T, N> >, N> () (runs, fast_tag ());
229#endif
230
231#ifdef USE_UNBOUNDED_ARRAY
232    header ("vector<unbounded_array> safe");
233    bench_my_vector_add<ublas::vector<T, ublas::unbounded_array<T> >, N> () (runs, safe_tag ());
234
235    header ("vector<unbounded_array> fast");
236    bench_my_vector_add<ublas::vector<T, ublas::unbounded_array<T> >, N> () (runs, fast_tag ());
237#endif
238
239#ifdef USE_STD_VALARRAY
240    header ("vector<std::valarray> safe");
241    bench_my_vector_add<ublas::vector<T, std::valarray<T> >, N> () (runs, safe_tag ());
242
243    header ("vector<std::valarray> fast");
244    bench_my_vector_add<ublas::vector<T, std::valarray<T> >, N> () (runs, fast_tag ());
245#endif
246
247#ifdef USE_STD_VECTOR
248    header ("vector<std::vector> safe");
249    bench_my_vector_add<ublas::vector<T, std::vector<T> >, N> () (runs, safe_tag ());
250
251    header ("vector<std::vector> fast");
252    bench_my_vector_add<ublas::vector<T, std::vector<T> >, N> () (runs, fast_tag ());
253#endif
254
255#ifdef USE_STD_VALARRAY
256    header ("std::valarray");
257    bench_cpp_vector_add<std::valarray<T>, N> () (runs);
258#endif
259}
260
261#ifdef USE_FLOAT
262template struct bench_1<float, 3>;
263template struct bench_1<float, 10>;
264template struct bench_1<float, 30>;
265template struct bench_1<float, 100>;
266#endif
267
268#ifdef USE_DOUBLE
269template struct bench_1<double, 3>;
270template struct bench_1<double, 10>;
271template struct bench_1<double, 30>;
272template struct bench_1<double, 100>;
273#endif
274
275#ifdef USE_STD_COMPLEX
276#ifdef USE_FLOAT
277template struct bench_1<std::complex<float>, 3>;
278template struct bench_1<std::complex<float>, 10>;
279template struct bench_1<std::complex<float>, 30>;
280template struct bench_1<std::complex<float>, 100>;
281#endif
282
283#ifdef USE_DOUBLE
284template struct bench_1<std::complex<double>, 3>;
285template struct bench_1<std::complex<double>, 10>;
286template struct bench_1<std::complex<double>, 30>;
287template struct bench_1<std::complex<double>, 100>;
288#endif
289#endif
Note: See TracBrowser for help on using the repository browser.