timemory 3.3.0
Modular C++ Toolkit for Performance Analysis and Logging. Profiling API and Tools for C, C++, CUDA, Fortran, and Python. The C++ template API is essentially a framework to creating tools: it is designed to provide a unifying interface for recording various performance measurements alongside data logging and interfaces to other tools.
tim::statistics< Tp > Struct Template Reference

A generic class for statistical accumulation. It uses the timemory math overloads to enable statistics for containers such as std::vector<double>, etc. More...

#include "timemory/data/statistics.hpp"

+ Collaboration diagram for tim::statistics< Tp >:

Public Types

using value_type = Tp
 
using this_type = statistics< Tp >
 
using compute_type = math::compute< Tp, Tp >
 
template<typename Vp >
using compute_value_t = math::compute< Tp, Vp >
 

Public Member Functions

 statistics ()=default
 
 ~statistics ()=default
 
 statistics (const statistics &)=default
 
 statistics (statistics &&) noexcept=default
 
statisticsoperator= (const statistics &)=default
 
statisticsoperator= (statistics &&) noexcept=default
 
 statistics (const value_type &val)
 
 statistics (value_type &&val)
 
statisticsoperator= (const value_type &val)
 
int64_t get_count () const
 
const value_typeget_min () const
 
const value_typeget_max () const
 
const value_typeget_sum () const
 
const value_typeget_sqr () const
 
value_type get_mean () const
 
value_type get_variance () const
 
value_type get_stddev () const
 
void reset ()
 
statisticsoperator+= (const value_type &val)
 
statisticsoperator-= (const value_type &val)
 
statisticsoperator*= (const value_type &val)
 
statisticsoperator/= (const value_type &val)
 
statisticsoperator+= (const statistics &rhs)
 
statisticsoperator-= (const statistics &rhs)
 
template<typename Archive >
void save (Archive &ar, const unsigned int) const
 
template<typename Archive >
void load (Archive &ar, const unsigned int)
 

Friends

std::ostream & operator<< (std::ostream &os, const statistics &obj)
 
statistics operator+ (const statistics &lhs, const statistics &rhs)
 
statistics operator- (const statistics &lhs, const statistics &rhs)
 

Detailed Description

template<typename Tp>
struct tim::statistics< Tp >

A generic class for statistical accumulation. It uses the timemory math overloads to enable statistics for containers such as std::vector<double>, etc.

Template Parameters
Tpdata type for statistical accumulation

Definition at line 82 of file statistics.hpp.

Member Typedef Documentation

◆ compute_type

template<typename Tp >
using tim::statistics< Tp >::compute_type = math::compute<Tp, Tp>

Definition at line 87 of file statistics.hpp.

◆ compute_value_t

template<typename Tp >
template<typename Vp >
using tim::statistics< Tp >::compute_value_t = math::compute<Tp, Vp>

Definition at line 89 of file statistics.hpp.

◆ this_type

template<typename Tp >
using tim::statistics< Tp >::this_type = statistics<Tp>

Definition at line 86 of file statistics.hpp.

◆ value_type

template<typename Tp >
using tim::statistics< Tp >::value_type = Tp

Definition at line 85 of file statistics.hpp.

Constructor & Destructor Documentation

◆ statistics() [1/5]

template<typename Tp >
tim::statistics< Tp >::statistics ( )
inlinedefault

◆ ~statistics()

template<typename Tp >
tim::statistics< Tp >::~statistics ( )
inlinedefault

◆ statistics() [2/5]

template<typename Tp >
tim::statistics< Tp >::statistics ( const statistics< Tp > &  )
inlinedefault

◆ statistics() [3/5]

template<typename Tp >
tim::statistics< Tp >::statistics ( statistics< Tp > &&  )
inlinedefaultnoexcept

◆ statistics() [4/5]

template<typename Tp >
tim::statistics< Tp >::statistics ( const value_type val)
inlineexplicit

Definition at line 99 of file statistics.hpp.

100 : m_cnt(1)
101 , m_sum(val)
102 , m_sqr(compute_type::sqr(val))
103 , m_min(val)
104 , m_max(val)
105 {}
static decltype(auto) sqr(const type &_v)
Definition: compute.hpp:63

◆ statistics() [5/5]

template<typename Tp >
tim::statistics< Tp >::statistics ( value_type &&  val)
inlineexplicit

Definition at line 107 of file statistics.hpp.

108 : m_cnt(1)
109 , m_sum(std::move(val))
110 , m_sqr(compute_type::sqr(m_sum))
111 , m_min(m_sum)
112 , m_max(m_sum)
113 {}

Member Function Documentation

◆ get_count()

template<typename Tp >
int64_t tim::statistics< Tp >::get_count ( ) const
inline

Definition at line 127 of file statistics.hpp.

127{ return m_cnt; }

◆ get_max()

template<typename Tp >
const value_type & tim::statistics< Tp >::get_max ( ) const
inline

Definition at line 129 of file statistics.hpp.

129{ return m_max; }

Referenced by std::max().

◆ get_mean()

template<typename Tp >
value_type tim::statistics< Tp >::get_mean ( ) const
inline

Definition at line 132 of file statistics.hpp.

132{ return m_sum / m_cnt; }

Referenced by tim::statistics< Tp >::save().

◆ get_min()

template<typename Tp >
const value_type & tim::statistics< Tp >::get_min ( ) const
inline

Definition at line 128 of file statistics.hpp.

128{ return m_min; }

Referenced by std::min().

◆ get_sqr()

template<typename Tp >
const value_type & tim::statistics< Tp >::get_sqr ( ) const
inline

Definition at line 131 of file statistics.hpp.

131{ return m_sqr; }

◆ get_stddev()

template<typename Tp >
value_type tim::statistics< Tp >::get_stddev ( ) const
inline

Definition at line 156 of file statistics.hpp.

157 {
159 }
static decltype(auto) abs(const type &_v)
Definition: compute.hpp:58
static decltype(auto) sqrt(const type &_v)
Definition: compute.hpp:68
value_type get_variance() const
Definition: statistics.hpp:133

References tim::math::compute< Tp, Up >::abs(), tim::statistics< Tp >::get_variance(), and tim::math::compute< Tp, Up >::sqrt().

Referenced by tim::statistics< Tp >::save().

◆ get_sum()

template<typename Tp >
const value_type & tim::statistics< Tp >::get_sum ( ) const
inline

Definition at line 130 of file statistics.hpp.

130{ return m_sum; }

◆ get_variance()

template<typename Tp >
value_type tim::statistics< Tp >::get_variance ( ) const
inline

Definition at line 133 of file statistics.hpp.

134 {
135 if(m_cnt < 2)
136 {
137 auto ret = m_sum;
138 compute_type::minus(ret, m_sum);
139 return ret;
140 }
141
142 auto _sum = m_sum;
143 auto _sqr = m_sqr;
144
145 // lambda for equation clarity (will be inlined)
146 auto compute_variance = [&]() {
147 compute_type::multiply(_sum, m_sum);
148 _sum /= m_cnt;
149 compute_type::minus(_sqr, _sum);
150 _sqr /= (m_cnt - 1);
151 return _sqr;
152 };
153 return compute_variance();
154 }
static decltype(auto) minus(type &_l, const V &_r)
Definition: compute.hpp:99
static decltype(auto) multiply(type &_l, const V &_r)
Definition: compute.hpp:105

References tim::math::compute< Tp, Up >::minus(), and tim::math::compute< Tp, Up >::multiply().

Referenced by tim::statistics< Tp >::get_stddev().

◆ load()

template<typename Tp >
template<typename Archive >
void tim::statistics< Tp >::load ( Archive &  ar,
const unsigned int   
)
inline

Definition at line 302 of file statistics.hpp.

303 {
304 ar(cereal::make_nvp("sum", m_sum), cereal::make_nvp("min", m_min),
305 cereal::make_nvp("max", m_max), cereal::make_nvp("sqr", m_sqr),
306 cereal::make_nvp("count", m_cnt));
307 }

◆ operator*=()

template<typename Tp >
statistics & tim::statistics< Tp >::operator*= ( const value_type val)
inline

Definition at line 205 of file statistics.hpp.

206 {
207 compute_type::multiply(m_sum, val);
209 compute_type::multiply(m_min, val);
210 compute_type::multiply(m_max, val);
211 return *this;
212 }

References tim::math::compute< Tp, Up >::multiply(), and tim::math::compute< Tp, Up >::sqr().

◆ operator+=() [1/2]

template<typename Tp >
statistics & tim::statistics< Tp >::operator+= ( const statistics< Tp > &  rhs)
inline

Definition at line 225 of file statistics.hpp.

226 {
227 if(m_cnt == 0)
228 {
229 m_sum = rhs.m_sum;
230 m_sqr = rhs.m_sqr;
231 m_min = rhs.m_min;
232 m_max = rhs.m_max;
233 }
234 else
235 {
236 compute_type::plus(m_sum, rhs.m_sum);
237 compute_type::plus(m_sqr, rhs.m_sqr);
238 m_min = compute_type::min(m_min, rhs.m_min);
239 m_max = compute_type::max(m_max, rhs.m_max);
240 }
241 m_cnt += rhs.m_cnt;
242 return *this;
243 }
static decltype(auto) max(const type &_l, const V &_r)
Definition: compute.hpp:80
static decltype(auto) min(const type &_l, const V &_r)
Definition: compute.hpp:74
static decltype(auto) plus(type &_l, const V &_r)
Definition: compute.hpp:93

References tim::math::compute< Tp, Up >::max(), tim::math::compute< Tp, Up >::min(), and tim::math::compute< Tp, Up >::plus().

◆ operator+=() [2/2]

template<typename Tp >
statistics & tim::statistics< Tp >::operator+= ( const value_type val)
inline

Definition at line 173 of file statistics.hpp.

174 {
175 if(m_cnt == 0)
176 {
177 m_sum = val;
178 m_sqr = compute_type::sqr(val);
179 m_min = val;
180 m_max = val;
181 }
182 else
183 {
184 compute_type::plus(m_sum, val);
186 m_min = compute_type::min(m_min, val);
187 m_max = compute_type::max(m_max, val);
188 }
189 ++m_cnt;
190
191 return *this;
192 }

References tim::math::compute< Tp, Up >::max(), tim::math::compute< Tp, Up >::min(), tim::math::compute< Tp, Up >::plus(), and tim::math::compute< Tp, Up >::sqr().

◆ operator-=() [1/2]

template<typename Tp >
statistics & tim::statistics< Tp >::operator-= ( const statistics< Tp > &  rhs)
inline

Definition at line 246 of file statistics.hpp.

247 {
248 if(m_cnt > 0)
249 {
250 compute_type::minus(m_sum, rhs.m_sum);
251 compute_type::minus(m_sqr, rhs.m_sqr);
252 m_min = compute_type::min(m_min, rhs.m_min);
253 m_max = compute_type::max(m_max, rhs.m_max);
254 // m_cnt += std::abs(m_cnt - rhs.m_cnt);
255 }
256 return *this;
257 }

References tim::math::compute< Tp, Up >::max(), tim::math::compute< Tp, Up >::min(), and tim::math::compute< Tp, Up >::minus().

◆ operator-=() [2/2]

template<typename Tp >
statistics & tim::statistics< Tp >::operator-= ( const value_type val)
inline

Definition at line 194 of file statistics.hpp.

195 {
196 if(m_cnt > 1)
197 --m_cnt;
198 compute_type::minus(m_sum, val);
200 compute_type::minus(m_min, val);
201 compute_type::minus(m_max, val);
202 return *this;
203 }

References tim::math::compute< Tp, Up >::minus(), and tim::math::compute< Tp, Up >::sqr().

◆ operator/=()

template<typename Tp >
statistics & tim::statistics< Tp >::operator/= ( const value_type val)
inline

Definition at line 214 of file statistics.hpp.

215 {
216 compute_type::divide(m_sum, val);
218 compute_type::divide(m_min, val);
219 compute_type::divide(m_max, val);
220 return *this;
221 }
static decltype(auto) divide(type &_l, const V &_r)
Definition: compute.hpp:111

References tim::math::compute< Tp, Up >::divide(), and tim::math::compute< Tp, Up >::sqr().

◆ operator=() [1/3]

template<typename Tp >
statistics & tim::statistics< Tp >::operator= ( const statistics< Tp > &  )
inlinedefault

◆ operator=() [2/3]

template<typename Tp >
statistics & tim::statistics< Tp >::operator= ( const value_type val)
inline

Definition at line 115 of file statistics.hpp.

116 {
117 m_cnt = 1;
118 m_sum = val;
119 m_min = val;
120 m_max = val;
121 m_sqr = compute_type::sqr(val);
122 return *this;
123 }

References tim::math::compute< Tp, Up >::sqr().

◆ operator=() [3/3]

template<typename Tp >
statistics & tim::statistics< Tp >::operator= ( statistics< Tp > &&  )
inlinedefaultnoexcept

◆ reset()

template<typename Tp >
void tim::statistics< Tp >::reset ( )
inline

Definition at line 162 of file statistics.hpp.

163 {
164 m_cnt = 0;
165 m_sum = value_type{};
166 m_sqr = value_type{};
167 m_min = value_type{};
168 m_max = value_type{};
169 }

◆ save()

template<typename Tp >
template<typename Archive >
void tim::statistics< Tp >::save ( Archive &  ar,
const unsigned int   
) const
inline

Definition at line 292 of file statistics.hpp.

293 {
294 auto _mean = (m_cnt > 0) ? get_mean() : value_type{};
295 ar(cereal::make_nvp("sum", m_sum), cereal::make_nvp("count", m_cnt),
296 cereal::make_nvp("min", m_min), cereal::make_nvp("max", m_max),
297 cereal::make_nvp("sqr", m_sqr), cereal::make_nvp("mean", _mean),
298 cereal::make_nvp("stddev", get_stddev()));
299 }
value_type get_mean() const
Definition: statistics.hpp:132
value_type get_stddev() const
Definition: statistics.hpp:156

References tim::statistics< Tp >::get_mean(), and tim::statistics< Tp >::get_stddev().

Friends And Related Function Documentation

◆ operator+

template<typename Tp >
statistics operator+ ( const statistics< Tp > &  lhs,
const statistics< Tp > &  rhs 
)
friend

Definition at line 281 of file statistics.hpp.

282 {
283 return statistics(lhs) += rhs;
284 }
statistics()=default

◆ operator-

template<typename Tp >
statistics operator- ( const statistics< Tp > &  lhs,
const statistics< Tp > &  rhs 
)
friend

Definition at line 286 of file statistics.hpp.

287 {
288 return statistics(lhs) -= rhs;
289 }

◆ operator<<

template<typename Tp >
std::ostream & operator<< ( std::ostream &  os,
const statistics< Tp > &  obj 
)
friend

Definition at line 269 of file statistics.hpp.

270 {
271 using namespace tim::stl::ostream;
272 auto _mean = (obj.get_count() > 0) ? obj.get_mean() : value_type{};
273 os << "[sum: " << obj.get_sum() << "] [mean: " << _mean
274 << "] [min: " << obj.get_min() << "] [max: " << obj.get_max()
275 << "] [var: " << obj.get_variance() << "] [stddev: " << obj.get_stddev()
276 << "] [count: " << obj.get_count() << "]";
277 return os;
278 }
the namespace provides overloads to output complex data types w/ streams
const std::string std::ostream * os

The documentation for this struct was generated from the following file: