xref: /aosp_15_r20/external/eigen/bench/bench_move_semantics.cpp (revision bf2c37156dfe67e5dfebd6d394bad8b2ab5804d4)
1 // This file is part of Eigen, a lightweight C++ template library
2 // for linear algebra.
3 //
4 // Copyright (C) 2020 Sebastien Boisvert <[email protected]>
5 //
6 // This Source Code Form is subject to the terms of the Mozilla
7 // Public License v. 2.0. If a copy of the MPL was not distributed
8 // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 
10 #include "BenchTimer.h"
11 #include "../test/MovableScalar.h"
12 
13 #include <Eigen/Core>
14 
15 #include <iostream>
16 #include <utility>
17 
18 template <typename MatrixType>
copy_matrix(MatrixType & m)19 void copy_matrix(MatrixType& m)
20 {
21   MatrixType tmp(m);
22   m = tmp;
23 }
24 
25 template <typename MatrixType>
move_matrix(MatrixType && m)26 void move_matrix(MatrixType&& m)
27 {
28   MatrixType tmp(std::move(m));
29   m = std::move(tmp);
30 }
31 
32 template<typename Scalar>
bench(const std::string & label)33 void bench(const std::string& label)
34 {
35   using MatrixType = Eigen::Matrix<Eigen::MovableScalar<Scalar>,1,10>;
36   Eigen::BenchTimer t;
37 
38   int tries = 10;
39   int rep = 1000000;
40 
41   MatrixType data = MatrixType::Random().eval();
42   MatrixType dest;
43 
44   BENCH(t, tries, rep, copy_matrix(data));
45   std::cout << label << " copy semantics: " << 1e3*t.best(Eigen::CPU_TIMER) << " ms" << std::endl;
46 
47   BENCH(t, tries, rep, move_matrix(std::move(data)));
48   std::cout << label << " move semantics: " << 1e3*t.best(Eigen::CPU_TIMER) << " ms" << std::endl;
49 }
50 
main()51 int main()
52 {
53   bench<float>("float");
54   bench<double>("double");
55   return 0;
56 }
57 
58