📄 bench22.cpp
字号:
//// Copyright (c) 2000-2002// Joerg Walter, Mathias Koch//// 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)//// The authors gratefully acknowledge the support of// GeNeSys mbH & Co. KG in producing this work.//#include "bench2.hpp"template<class T, int N>struct bench_c_outer_prod { typedef T value_type; void operator () (int runs) const { try { static typename c_matrix_traits<T, N, N>::type m; static typename c_vector_traits<T, N>::type v1, v2; initialize_c_vector<T, N> () (v1); initialize_c_vector<T, N> () (v2); boost::timer t; for (int i = 0; i < runs; ++ i) { for (int j = 0; j < N; ++ j) { for (int k = 0; k < N; ++ k) { m [j] [k] = - v1 [j] * v2 [k]; } }// sink_c_matrix<T, N, N> () (m); } footer<value_type> () (N * N, N * N, runs, t.elapsed ()); } catch (std::exception &e) { std::cout << e.what () << std::endl; } }};template<class M, class V, int N>struct bench_my_outer_prod { typedef typename M::value_type value_type; void operator () (int runs, safe_tag) const { try { static M m (N, N, N * N); static V v1 (N, N), v2 (N, N); initialize_vector (v1); initialize_vector (v2); boost::timer t; for (int i = 0; i < runs; ++ i) { m = - ublas::outer_prod (v1, v2);// sink_matrix (m); } footer<value_type> () (N * N, N * N, runs, t.elapsed ()); } catch (std::exception &e) { std::cout << e.what () << std::endl; } } void operator () (int runs, fast_tag) const { try { static M m (N, N, N * N); static V v1 (N, N), v2 (N, N); initialize_vector (v1); initialize_vector (v2); boost::timer t; for (int i = 0; i < runs; ++ i) { m.assign (- ublas::outer_prod (v1, v2));// sink_matrix (m); } footer<value_type> () (N * N, N * N, runs, t.elapsed ()); } catch (std::exception &e) { std::cout << e.what () << std::endl; } }};template<class M, class V, int N>struct bench_cpp_outer_prod { typedef typename M::value_type value_type; void operator () (int runs) const { try { static M m (N * N); static V v1 (N), v2 (N); initialize_vector (v1); initialize_vector (v2); boost::timer t; for (int i = 0; i < runs; ++ i) { for (int j = 0; j < N; ++ j) { for (int k = 0; k < N; ++ k) { m [N * j + k] = - v1 [j] * v2 [k]; } }// sink_vector (m); } footer<value_type> () (N * N, N * N, runs, t.elapsed ()); } catch (std::exception &e) { std::cout << e.what () << std::endl; } }};template<class T, int N>struct bench_c_matrix_vector_prod { typedef T value_type; void operator () (int runs) const { try { static typename c_matrix_traits<T, N, N>::type m; static typename c_vector_traits<T, N>::type v1, v2; initialize_c_matrix<T, N, N> () (m); initialize_c_vector<T, N> () (v1); boost::timer t; for (int i = 0; i < runs; ++ i) { for (int j = 0; j < N; ++ j) { v2 [j] = 0; for (int k = 0; k < N; ++ k) { v2 [j] += m [j] [k] * v1 [k]; } }// sink_c_vector<T, N> () (v2); } footer<value_type> () (N * N, N * (N - 1), runs, t.elapsed ()); } catch (std::exception &e) { std::cout << e.what () << std::endl; } }};template<class M, class V, int N>struct bench_my_matrix_vector_prod { typedef typename M::value_type value_type; void operator () (int runs, safe_tag) const { try { static M m (N, N, N * N); static V v1 (N, N), v2 (N, N); initialize_matrix (m); initialize_vector (v1); boost::timer t; for (int i = 0; i < runs; ++ i) { v2 = ublas::prod (m, v1);// sink_vector (v2); } footer<value_type> () (N * N, N * (N - 1), runs, t.elapsed ()); } catch (std::exception &e) { std::cout << e.what () << std::endl; } } void operator () (int runs, fast_tag) const { try { static M m (N, N, N * N); static V v1 (N, N), v2 (N, N); initialize_matrix (m); initialize_vector (v1); boost::timer t; for (int i = 0; i < runs; ++ i) { v2.assign (ublas::prod (m, v1));// sink_vector (v2); } footer<value_type> () (N * N, N * (N - 1), runs, t.elapsed ()); } catch (std::exception &e) { std::cout << e.what () << std::endl; } }};template<class M, class V, int N>struct bench_cpp_matrix_vector_prod { typedef typename M::value_type value_type; void operator () (int runs) const { try { static M m (N * N); static V v1 (N), v2 (N); initialize_vector (m); initialize_vector (v1); boost::timer t; for (int i = 0; i < runs; ++ i) { for (int j = 0; j < N; ++ j) { std::valarray<value_type> row (m [std::slice (N * j, N, 1)]); v2 [j] = (row * v1).sum (); }// sink_vector (v2); } footer<value_type> () (N * N, N * (N - 1), runs, t.elapsed ()); } catch (std::exception &e) { std::cout << e.what () << std::endl; } }};template<class T, int N>struct bench_c_matrix_add { typedef T value_type; void operator () (int runs) const { try { static typename c_matrix_traits<T, N, N>::type m1, m2, m3; initialize_c_matrix<T, N, N> () (m1); initialize_c_matrix<T, N, N> () (m2); boost::timer t; for (int i = 0; i < runs; ++ i) { for (int j = 0; j < N; ++ j) { for (int k = 0; k < N; ++ k) { m3 [j] [k] = - (m1 [j] [k] + m2 [j] [k]); } }// sink_c_matrix<T, N, N> () (m3); } footer<value_type> () (0, 2 * N * N, runs, t.elapsed ()); } catch (std::exception &e) { std::cout << e.what () << std::endl; } }};template<class M, int N>struct bench_my_matrix_add { typedef typename M::value_type value_type; void operator () (int runs, safe_tag) const { try { static M m1 (N, N, N * N), m2 (N, N, N * N), m3 (N, N, N * N); initialize_matrix (m1); initialize_matrix (m2); boost::timer t;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -