Expression Templates Library (ETL)
timed_expr.hpp
Go to the documentation of this file.
1 //=======================================================================
2 // Copyright (c) 2014-2023 Baptiste Wicht
3 // Distributed under the terms of the MIT License.
4 // (See accompanying file LICENSE or copy at
5 // http://opensource.org/licenses/MIT)
6 //=======================================================================
7 
13 #pragma once
14 
15 #include <iosfwd> //For stream support
16 
17 #include "etl/wrapper_traits.hpp"
18 
19 namespace etl {
20 
24 template <typename Expr, typename R>
25 struct timed_expr final {
26  using clock_resolution = R;
27  using expr_t = Expr;
29 
30 private:
31  Expr value;
32 
33  friend struct wrapper_traits<timed_expr>;
34 
35 public:
36  //Cannot be constructed with no args
37  timed_expr() = delete;
38 
43  explicit timed_expr(Expr l) : value(std::forward<Expr>(l)) {
44  //Nothing else to init
45  }
46 
47  //Expresison can be copied and moved
48  timed_expr(const timed_expr& e) = default;
49  timed_expr(timed_expr&& e) noexcept = default;
50 
51  //Expressions are invariant
52  timed_expr& operator=(const timed_expr& e) = delete;
53  timed_expr& operator=(timed_expr&& e) = delete;
54 
60  template <typename E>
61  bool alias(const E& other) const noexcept {
62  return value.alias(other);
63  }
64 
65  // Assignment functions
66 
71  template <typename L>
72  void assign_to(L&& lhs) const {
73  auto start_time = etl::timer_clock::now();
74 
75  value.assign_to(lhs);
76 
77  auto end_time = etl::timer_clock::now();
78  auto duration = std::chrono::duration_cast<clock_resolution>(end_time - start_time);
79 
80  std::cout << "timed(=): " << value << " took " << duration.count() << resolution_to_string<clock_resolution>() << std::endl;
81  }
82 
87  template <typename L>
88  void assign_add_to(L&& lhs) const {
89  auto start_time = etl::timer_clock::now();
90 
91  value.assign_add_to(lhs);
92 
93  auto end_time = etl::timer_clock::now();
94  auto duration = std::chrono::duration_cast<clock_resolution>(end_time - start_time);
95 
96  std::cout << "timed(+=): " << value << " took " << duration.count() << resolution_to_string<clock_resolution>() << std::endl;
97  }
98 
103  template <typename L>
104  void assign_sub_to(L&& lhs) const {
105  auto start_time = etl::timer_clock::now();
106 
107  value.assign_sub_to(lhs);
108 
109  auto end_time = etl::timer_clock::now();
110  auto duration = std::chrono::duration_cast<clock_resolution>(end_time - start_time);
111 
112  std::cout << "timed(-=): " << value << " took " << duration.count() << resolution_to_string<clock_resolution>() << std::endl;
113  }
114 
119  template <typename L>
120  void assign_mul_to(L&& lhs) const {
121  auto start_time = etl::timer_clock::now();
122 
123  value.assign_mul_to(lhs);
124 
125  auto end_time = etl::timer_clock::now();
126  auto duration = std::chrono::duration_cast<clock_resolution>(end_time - start_time);
127 
128  std::cout << "timed(*=): " << value << " took " << duration.count() << resolution_to_string<clock_resolution>() << std::endl;
129  }
130 
135  template <typename L>
136  void assign_div_to(L&& lhs) const {
137  auto start_time = etl::timer_clock::now();
138 
139  value.assign_div_to(lhs);
140 
141  auto end_time = etl::timer_clock::now();
142  auto duration = std::chrono::duration_cast<clock_resolution>(end_time - start_time);
143 
144  std::cout << "timed(/=): " << value << " took " << duration.count() << resolution_to_string<clock_resolution>() << std::endl;
145  }
146 
151  template <typename L>
152  void assign_mod_to(L&& lhs) const {
153  auto start_time = etl::timer_clock::now();
154 
155  value.assign_mod_to(lhs);
156 
157  auto end_time = etl::timer_clock::now();
158  auto duration = std::chrono::duration_cast<clock_resolution>(end_time - start_time);
159 
160  std::cout << "timed(%=): " << value << " took " << duration.count() << resolution_to_string<clock_resolution>() << std::endl;
161  }
162 
169  friend std::ostream& operator<<(std::ostream& os, const timed_expr& expr) {
170  return os << "timed(" << expr.value << ")";
171  }
172 };
173 
178 template <typename Expr, typename R>
179 struct etl_traits<etl::timed_expr<Expr, R>> : wrapper_traits<etl::timed_expr<Expr, R>> {};
180 
181 } //end of namespace etl
R clock_resolution
The clock resolution.
Definition: timed_expr.hpp:26
Contains traits for wrapper expressions.
bool alias(const E &other) const noexcept
Test if this expression aliases with the given expression.
Definition: timed_expr.hpp:61
friend std::ostream & operator<<(std::ostream &os, const timed_expr &expr)
Prints the type of the timed expression to the stream.
Definition: timed_expr.hpp:169
Traits to get information about ETL types.
Definition: tmp.hpp:68
Root namespace for the ETL library.
Definition: adapter.hpp:15
void assign_mul_to(L &&lhs) const
Multiply the given left-hand-side expression.
Definition: timed_expr.hpp:120
void assign_to(L &&lhs) const
Assign to the given left-hand-side expression.
Definition: timed_expr.hpp:72
timed_expr(Expr l)
Construt a new timed expression around the given ETL expression.
Definition: timed_expr.hpp:43
void assign_mod_to(L &&lhs) const
Modulo the given left-hand-side expression.
Definition: timed_expr.hpp:152
Expr expr_t
The wrapped expression type.
Definition: timed_expr.hpp:27
value_t< Expr > value_type
The value type.
Definition: timed_expr.hpp:28
void assign_add_to(L &&lhs) const
Add to the given left-hand-side expression.
Definition: timed_expr.hpp:88
void assign_sub_to(L &&lhs) const
Sub from the given left-hand-side expression.
Definition: timed_expr.hpp:104
void assign_div_to(L &&lhs) const
Divide the given left-hand-side expression.
Definition: timed_expr.hpp:136
A wrapper for expressions that need to be timed.
Definition: timed_expr.hpp:25
typename decay_traits< E >::value_type value_t
Traits to extract the value type out of an ETL type.
Definition: tmp.hpp:81
Traits for wrapper expressions.
Definition: wrapper_traits.hpp:21