ToolDAQFramework
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
Stopwatch.cpp
Go to the documentation of this file.
1 #include "Stopwatch.h"
2 
3 #include <sstream>
4 #include <iostream>
5 #include <algorithm>
6 
7 #include "TH1D.h"
8 #include "TLegend.h"
9 #include "TCanvas.h"
10 
12 util::Stopwatch::Stopwatch(const char * tool_name)
13  : m_tool_name(tool_name)
14 {
15 }
16 
19  m_running = true;
20  m_sw.Start();
21 }
22 
25  m_sw.Stop();
26  m_running = false;
27  StopwatchTimes times;
28  times.cpu_time = m_sw.CpuTime();
29  times.real_time = m_sw.RealTime();
30 
31  //append to the results
32  m_results.push_back(times);
33 
34  return times;
35 }
36 
38 std::string util::Stopwatch::Result(std::string method_name, std::string output_file) {
39  //If it's running, stop it
40  if(m_running)
41  this->Stop();
42 
43  //get the stats
44  double min_cpu = 9999, max_cpu = -9999, tot_cpu = 0;
45  double min_real = 9999, max_real = -9999, tot_real = 0;
46  double cpu_var = 0.;
47  double real_var = 0.;
48  double cpu, real;
49  const int n = m_results.size();
50  for(size_t i = 0; i < n; i++) {
51  cpu = m_results[i].cpu_time;
52  real = m_results[i].real_time;
53  //increment totals
54  tot_cpu += cpu;
55  tot_real += real;
56  //find mins
57  min_cpu = cpu < min_cpu ? cpu : min_cpu;
58  min_real = real < min_real ? real : min_real;
59  //and maxes
60  max_cpu = cpu > max_cpu ? cpu : max_cpu;
61  max_real = real > max_real ? real : max_real;
62  }//i
63  //Create, fill, and save a histogram
64  if(output_file.length()) {
65  double min = std::min(min_cpu, min_real);
66  double max = std::max(max_cpu, max_real);
67  TH1D hcpu ("hcpu", ";Time (s);Runs", sqrt(n), min, max);
68  TH1D hreal("hreal", ";Time (s);Runs", sqrt(n), min, max);
69  for(size_t i = 0; i < n; i++) {
70  cpu_var += pow((m_results[i].cpu_time-tot_cpu/n),2.)/(n-1.);
71  hcpu .Fill(m_results[i].cpu_time);
72  real_var += pow((m_results[i].real_time-tot_real/n),2.)/(n-1.);
73  hreal.Fill(m_results[i].real_time);
74  }//i
75  TCanvas c;
76  hcpu.GetYaxis()->SetRangeUser(0, 1.05 * std::max(hcpu.GetMaximum(), hreal.GetMaximum()));
77  hcpu.Draw();
78  hreal.SetLineColor(kRed);
79  hreal.Draw("SAME");
80  TLegend leg(0.8, 0.9, 1, 1);
81  leg.AddEntry(&hcpu, "CPU time", "L");
82  leg.AddEntry(&hreal, "Real time", "L");
83  leg.Draw();
84  c.SaveAs(output_file.c_str());
85  }
86 
87  //format the output
88  std::stringstream ss;
89  ss << m_tool_name << "::" << method_name << "() run timing stats:" << std::endl
90  << " CPU Time (s): Total = " << tot_cpu << std::endl
91  << " Average = " << tot_cpu / (double)n << std::endl
92  << " Sample standard deviation = " << sqrt(cpu_var)
93  << " over " << n << " runs" << std::endl
94  << " Range of " << min_cpu << " to " << max_cpu << std::endl
95  << " Real Time (s): Total = " << tot_real << std::endl
96  << " Average = " << tot_real / (double)n << std::endl
97  << " Sample standard deviation = " << sqrt(real_var)
98  << " over " << n << " runs" << std::endl
99  << " Range of " << min_real << " to " << max_real << std::endl;
100 
101  //reset
102  this->Reset();
103 
104  return ss.str();
105 }
106 
109  if(m_running)
110  m_sw.Stop();
111  m_results.clear();
112 }
113 
std::string Result(std::string method_name, std::string output_file="")
Definition: Stopwatch.cpp:38
void Start()
Start the stopwatch.
Definition: Stopwatch.cpp:18
StopwatchTimes Stop()
Stop the stopwatch, returning the CPU time.
Definition: Stopwatch.cpp:24
Stopwatch(const char *tool_name)
Definition: Stopwatch.cpp:12
void Reset()
Stop the stopwatch, and clear the results vector.
Definition: Stopwatch.cpp:108
std::string output_file
Definition: library_daq.h:200