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 t_end = std::chrono::high_resolution_clock::now();
17 t_duration += std::chrono::duration<double, std::milli>(t_end - t_start).count();
18 }
19 double count() const {
20 auto now = std::chrono::high_resolution_clock::now();
21 return std::chrono::duration<double, std::milli>(now - t_start).count();
22 }
23 void start() { t_start = std::chrono::high_resolution_clock::now(); }
24 operator double() const { return t_duration; };
25 };
26
27 template <int N, class FCN> double Profile(const FCN &fcn, const std::string &name = "") {
29 for(size_t i = 0; i < N; ++i) fcn();
30 t.stop();
31 INFO((name == "" ? type_string<FCN>() : name) << " " << t / double(N) << "[ms] per iteration");
32 return t;
33 }
34 template <int N, class FCN> double ProfileWithStat(const FCN &fcn, const std::string &name = "") {
35 double t = 0;
36 double t2 = 0;
37 double tmin = 1e9;
38 double tmax = 0;
39 for(size_t i = 0; i < N; ++i) {
40 ProfileClock pi;
41 fcn();
42 pi.stop();
43 t += pi;
44 t2 += pi * pi;
45 tmin = pi < tmin ? pi : tmin;
46 tmax = pi > tmax ? pi : tmax;
47 }
48 t /= double(N);
49 t2 = std::sqrt(t2 / double(N) - t * t);
50 INFO((name == "" ? type_string<FCN>() : name) << " " << t << " ± " << t2 << "[ms] per iteration << [" << tmin << ", " << tmax << "]");
51 return t;
52 }
53
54 template <int N, class FCN> double Profile2(const FCN &fcn) {
56 auto z = 0;
57 for(size_t i = 0; i < N; ++i) z += fcn();
58 t.stop();
59 INFO(type_string<FCN>() << " " << t / double(N) << "[ms] per iteration; " << z);
60 return t;
61 }
62}
63#endif
#define INFO(X)
Used for printing information messages, and will always be printed.
Definition MsgService.h:81
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:17
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