⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 bench32.cpp

📁 C++的一个好库。。。现在很流行
💻 CPP
📖 第 1 页 / 共 2 页
字号:
//
//  Copyright (c) 2000-2002
//  Joerg Walter, Mathias Koch
//
//  Permission to use, copy, modify, distribute and sell this software
//  and its documentation for any purpose is hereby granted without fee,
//  provided that the above copyright notice appear in all copies and
//  that both that copyright notice and this permission notice appear
//  in supporting documentation.  The authors make no representations
//  about the suitability of this software for any purpose.
//  It is provided "as is" without express or implied warranty.
//
//  The authors gratefully acknowledge the support of
//  GeNeSys mbH & Co. KG in producing this work.
//

#include "bench3.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);
            ublas::matrix_range<M> mr (m, ublas::range (0, N), ublas::range (0, N));
            static V v1 (N), v2 (N);
            ublas::vector_range<V> vr1 (v1, ublas::range (0, N)),
                                   vr2 (v2, ublas::range (0, N));
            initialize_vector (vr1);
            initialize_vector (vr2);
            boost::timer t;
            for (int i = 0; i < runs; ++ i) {
                mr = - ublas::outer_prod (vr1, vr2);
//                sink_matrix (mr);
            }
            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);
            ublas::matrix_range<M> mr (m, ublas::range (0, N), ublas::range (0, N));
            static V v1 (N), v2 (N);
            ublas::vector_range<V> vr1 (v1, ublas::range (0, N)),
                                   vr2 (v2, ublas::range (0, N));
            initialize_vector (vr1);
            initialize_vector (vr2);
            boost::timer t;
            for (int i = 0; i < runs; ++ i) {
                mr.assign (- ublas::outer_prod (vr1, vr2));
//                sink_matrix (mr);
            }
            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);
            ublas::matrix_range<M> mr (m, ublas::range (0, N), ublas::range (0, N));
            static V v1 (N), v2 (N);
            ublas::vector_range<V> vr1 (v1, ublas::range (0, N)),
                                   vr2 (v2, ublas::range (0, N));
            initialize_matrix (mr);
            initialize_vector (vr1);
            boost::timer t;
            for (int i = 0; i < runs; ++ i) {
                vr2 = ublas::prod (mr, vr1);
//                sink_vector (vr2);
            }
            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);
            ublas::matrix_range<M> mr (m, ublas::range (0, N), ublas::range (0, N));
            static V v1 (N), v2 (N);
            ublas::vector_range<V> vr1 (v1, ublas::range (0, N)),
                                   vr2 (v2, ublas::range (0, N));
            initialize_matrix (mr);
            initialize_vector (vr1);
            boost::timer t;
            for (int i = 0; i < runs; ++ i) {
                vr2.assign (ublas::prod (mr, vr1));
//                sink_vector (vr2);
            }
            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), m2 (N, N), m3 (N, N);
            initialize_matrix (m1);
            initialize_matrix (m2);
            boost::timer t;
            for (int i = 0; i < runs; ++ i) {
                m3 = - (m1 + m2);
//                sink_matrix (m3);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -