OpenShot Library | OpenShotAudio  0.2.2
juce_PerformanceCounter.h
1 
2 /** @weakgroup juce_core-time
3  * @{
4  */
5 /*
6  ==============================================================================
7 
8  This file is part of the JUCE library.
9  Copyright (c) 2017 - ROLI Ltd.
10 
11  JUCE is an open source library subject to commercial or open-source
12  licensing.
13 
14  The code included in this file is provided under the terms of the ISC license
15  http://www.isc.org/downloads/software-support-policy/isc-license. Permission
16  To use, copy, modify, and/or distribute this software for any purpose with or
17  without fee is hereby granted provided that the above copyright notice and
18  this permission notice appear in all copies.
19 
20  JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
21  EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
22  DISCLAIMED.
23 
24  ==============================================================================
25 */
26 
27 namespace juce
28 {
29 
30 //==============================================================================
31 /** A timer for measuring performance of code and dumping the results to a file.
32 
33  e.g. @code
34 
35  PerformanceCounter pc ("fish", 50, "/temp/myfishlog.txt");
36 
37  for (;;)
38  {
39  pc.start();
40 
41  doSomethingFishy();
42 
43  pc.stop();
44  }
45  @endcode
46 
47  In this example, the time of each period between calling start/stop will be
48  measured and averaged over 50 runs, and the results printed to a file
49  every 50 times round the loop.
50 
51  @tags{Core}
52 */
54 {
55 public:
56  //==============================================================================
57  /** Creates a PerformanceCounter object.
58 
59  @param counterName the name used when printing out the statistics
60  @param runsPerPrintout the number of start/stop iterations before calling
61  printStatistics()
62  @param loggingFile a file to dump the results to - if this is File(),
63  the results are just written to the debugger output
64  */
65  PerformanceCounter (const String& counterName,
66  int runsPerPrintout = 100,
67  const File& loggingFile = File());
68 
69  /** Destructor. */
71 
72  //==============================================================================
73  /** Starts timing.
74  @see stop
75  */
76  void start() noexcept;
77 
78  /** Stops timing and prints out the results.
79 
80  The number of iterations before doing a printout of the
81  results is set in the constructor.
82 
83  @see start
84  */
85  bool stop();
86 
87  /** Dumps the current metrics to the debugger output and to a file.
88 
89  As well as using Logger::outputDebugString to print the results,
90  this will write then to the file specified in the constructor (if
91  this was valid).
92  */
93  void printStatistics();
94 
95  /** Holds the current statistics. */
96  struct Statistics
97  {
98  Statistics() noexcept;
99 
100  void clear() noexcept;
101  String toString() const;
102 
103  void addResult (double elapsed) noexcept;
104 
105  String name;
106  double averageSeconds;
107  double maximumSeconds;
108  double minimumSeconds;
109  double totalSeconds;
110  int64 numRuns;
111  };
112 
113  /** Returns a copy of the current stats, and resets the internal counter. */
114  Statistics getStatisticsAndReset();
115 
116 private:
117  //==============================================================================
118  Statistics stats;
119  int64 runsPerPrint, startTime;
120  File outputFile;
121 
122  JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (PerformanceCounter)
123 };
124 
125 
126 //==============================================================================
127 /**
128  Simple RAII class for measuring the time spent in a scope.
129 
130  Example:
131 
132  {
133  double timeSec;
134 
135  {
136  ScopedTimeMeasurement m (timeSec);
137  doSomething();
138  }
139 
140  Logger::writeToLog ("doSomething() took " + String (timeSec) + "seconds");
141  }
142 
143  @param resultInSeconds The result of the measurement will be stored in this variable.
144 
145  @tags{Core}
146 */
148 {
149 public:
150  ScopedTimeMeasurement (double& resultInSeconds) noexcept
151  : result (resultInSeconds)
152  {
153  result = 0.0;
154  }
155 
157  {
158  static auto scaler = 1.0 / static_cast<double> (Time::getHighResolutionTicksPerSecond());
159  result = static_cast<double> (Time::getHighResolutionTicks() - startTimeTicks) * scaler;
160  }
161 
162 private:
163  int64 startTimeTicks = Time::getHighResolutionTicks();
164  double& result;
165 
166  JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (ScopedTimeMeasurement)
167 };
168 
169 } // namespace juce
170 
171 /** @}*/
Represents a local file or directory.
Definition: juce_File.h:45
A timer for measuring performance of code and dumping the results to a file.
Simple RAII class for measuring the time spent in a scope.
The JUCE String class!
Definition: juce_String.h:43
static int64 getHighResolutionTicks() noexcept
Returns the current high-resolution counter's tick-count.
static int64 getHighResolutionTicksPerSecond() noexcept
Returns the resolution of the high-resolution counter in ticks per second.
#define JUCE_API
This macro is added to all JUCE public class declarations.