aboutsummaryrefslogtreecommitdiffhomepage
path: root/bench
diff options
context:
space:
mode:
authorGravatar Sebastien Boisvert <seb@boisvert.info>2020-06-11 23:43:25 +0000
committerGravatar Rasmus Munk Larsen <rmlarsen@google.com>2020-06-11 23:43:25 +0000
commit39cbd6578fbf3a98d8a213c8ec3f5147557d065e (patch)
treecc04e0117d47268dbbf3a79cd851a7bcf7d5b1f0 /bench
parenta7d2552af8b34f6befba9988c36fe5d9723892e6 (diff)
Fix #1911: add benchmark for move semantics with fixed-size matrix
$ clang++ -O3 bench/bench_move_semantics.cpp -I. -std=c++11 \ -o bench_move_semantics $ ./bench_move_semantics float copy semantics: 1755.97 ms float move semantics: 55.063 ms double copy semantics: 2457.65 ms double move semantics: 55.034 ms
Diffstat (limited to 'bench')
-rw-r--r--bench/bench_move_semantics.cpp57
1 files changed, 57 insertions, 0 deletions
diff --git a/bench/bench_move_semantics.cpp b/bench/bench_move_semantics.cpp
new file mode 100644
index 000000000..323d80417
--- /dev/null
+++ b/bench/bench_move_semantics.cpp
@@ -0,0 +1,57 @@
+// This file is part of Eigen, a lightweight C++ template library
+// for linear algebra.
+//
+// Copyright (C) 2020 Sebastien Boisvert <seb@boisvert.info>
+//
+// This Source Code Form is subject to the terms of the Mozilla
+// Public License v. 2.0. If a copy of the MPL was not distributed
+// with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
+
+#include "BenchTimer.h"
+#include "../test/MovableScalar.h"
+
+#include <Eigen/Core>
+
+#include <iostream>
+#include <utility>
+
+template <typename MatrixType>
+void copy_matrix(MatrixType& m)
+{
+ MatrixType tmp(m);
+ m = tmp;
+}
+
+template <typename MatrixType>
+void move_matrix(MatrixType&& m)
+{
+ MatrixType tmp(std::move(m));
+ m = std::move(tmp);
+}
+
+template<typename Scalar>
+void bench(const std::string& label)
+{
+ using MatrixType = Eigen::Matrix<Eigen::MovableScalar<Scalar>,1,10>;
+ Eigen::BenchTimer t;
+
+ int tries = 10;
+ int rep = 1000000;
+
+ MatrixType data = MatrixType::Random().eval();
+ MatrixType dest;
+
+ BENCH(t, tries, rep, copy_matrix(data));
+ std::cout << label << " copy semantics: " << 1e3*t.best(Eigen::CPU_TIMER) << " ms" << std::endl;
+
+ BENCH(t, tries, rep, move_matrix(std::move(data)));
+ std::cout << label << " move semantics: " << 1e3*t.best(Eigen::CPU_TIMER) << " ms" << std::endl;
+}
+
+int main()
+{
+ bench<float>("float");
+ bench<double>("double");
+ return 0;
+}
+