AmpGen 2.1
Loading...
Searching...
No Matches
ProfileClock.h
Go to the documentation of this file.
1#ifndef AMPGEN_PROFILECLOCK_H
2#define AMPGEN_PROFILECLOCK_H 1
3#include <chrono>
4#include <cmath>
5#include "AmpGen/MsgService.h"
6#include "AmpGen/Utilities.h"
7
8namespace AmpGen{
9 struct ProfileClock {
10 std::chrono::time_point<std::chrono::high_resolution_clock> t_start;
11 std::chrono::time_point<std::chrono::high_resolution_clock> t_end;
12 double t_duration = {0};
13
14 ProfileClock() : t_start(std::chrono::high_resolution_clock::now()) {}
15 void stop()
16 {
17 t_end = std::chrono::high_resolution_clock::now() ;
18 t_duration += std::chrono::duration<double, std::milli>( t_end - t_start ).count();
19 }
20 double count() const
21 {
22 auto now = std::chrono::high_resolution_clock::now() ;
23 return std::chrono::duration<double, std::milli>(now - t_start ).count();
24 }
25 void start(){ t_start = std::chrono::high_resolution_clock::now() ; }
26 operator double() const { return t_duration; } ;
27 };
28
29 template <int N, class FCN>
30 double Profile( const FCN& fcn, const std::string& name ="" ){
31 ProfileClock t;
32 for( size_t i = 0 ; i < N; ++i ) fcn();
33 t.stop();
34 INFO( (name == "" ? type_string<FCN>() : name ) << " " << t/double(N) << "[ms] per iteration" );
35 return t;
36 }
37 template <int N, class FCN>
38 double ProfileWithStat( const FCN& fcn, const std::string& name ="" ){
39 double t = 0;
40 double t2 = 0;
41 double tmin = 1e9;
42 double tmax = 0;
43 for( size_t i = 0 ; i < N; ++i ){
44 ProfileClock pi;
45 fcn();
46 pi.stop();
47 t += pi;
48 t2 += pi*pi;
49 tmin = pi < tmin ? pi : tmin;
50 tmax = pi > tmax ? pi : tmax;
51 }
52 t /= double(N);
53 t2 = std::sqrt( t2 / double(N) - t*t);
54 INFO( (name == "" ? type_string<FCN>() : name ) << " " << t << " ± " << t2 << "[ms] per iteration << [" << tmin << ", " << tmax << "]" );
55 return t;
56 }
57
58 template <int N, class FCN>
59 double Profile2( const FCN& fcn ){
61 auto z = 0 ;
62 for( size_t i = 0 ; i < N; ++i ) z += fcn();
63 t.stop();
64 INFO( type_string<FCN>() << " " << t/double(N) << "[ms] per iteration; " << z );
65 return t;
66 }
67}
68#endif
#define INFO(X)
Used for printing information messages, and will always be printed.
Definition MsgService.h:75
double ProfileWithStat(const FCN &fcn, const std::string &name="")
double Profile(const FCN &fcn, const std::string &name="")
std::string type_string()
Utility classes for compile-time metaprogramming, such as identifying the types of arguments for gene...
Definition MetaUtils.h:18
double Profile2(const FCN &fcn)
double count() const
std::chrono::time_point< std::chrono::high_resolution_clock > t_end
std::chrono::time_point< std::chrono::high_resolution_clock > t_start