blob: 323d80417c0c87316c6f616ba031c4dfa01b8a6b [file] [log] [blame]
Austin Schuhc55b0172022-02-20 17:52:35 -08001// This file is part of Eigen, a lightweight C++ template library
2// for linear algebra.
3//
4// Copyright (C) 2020 Sebastien Boisvert <seb@boisvert.info>
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
18template <typename MatrixType>
19void copy_matrix(MatrixType& m)
20{
21 MatrixType tmp(m);
22 m = tmp;
23}
24
25template <typename MatrixType>
26void move_matrix(MatrixType&& m)
27{
28 MatrixType tmp(std::move(m));
29 m = std::move(tmp);
30}
31
32template<typename Scalar>
33void 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
51int main()
52{
53 bench<float>("float");
54 bench<double>("double");
55 return 0;
56}
57