ilqgames
A new real-time solver for large-scale differential games.
loop_timer.h
1 /*
2  * Copyright (c) 2019, The Regents of the University of California (Regents).
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are
7  * met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  *
12  * 2. Redistributions in binary form must reproduce the above
13  * copyright notice, this list of conditions and the following
14  * disclaimer in the documentation and/or other materials provided
15  * with the distribution.
16  *
17  * 3. Neither the name of the copyright holder nor the names of its
18  * contributors may be used to endorse or promote products derived
19  * from this software without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS AS IS
22  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
25  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31  * POSSIBILITY OF SUCH DAMAGE.
32  *
33  * Please contact the author(s) of this library if you have any questions.
34  * Authors: David Fridovich-Keil ( dfk@eecs.berkeley.edu )
35  */
36 
37 ///////////////////////////////////////////////////////////////////////////////
38 //
39 // Keeps track of elapsed time (e.g., during loops) and provides an upper bound
40 // on the runtime of the next loop. To reduce memory consumption and adapt to
41 // changing processor activity, computes statistics based on a moving window of
42 // specified length.
43 //
44 ///////////////////////////////////////////////////////////////////////////////
45 
46 #ifndef ILQGAMES_UTILS_LOOP_TIMER_H
47 #define ILQGAMES_UTILS_LOOP_TIMER_H
48 
49 #include <ilqgames/utils/types.h>
50 
51 #include <glog/logging.h>
52 #include <chrono>
53 #include <list>
54 
55 namespace ilqgames {
56 
57 class LoopTimer {
58  public:
59  ~LoopTimer() {}
60  LoopTimer(size_t max_samples = 10)
61  : max_samples_(max_samples), total_time_(0.0) {
62  CHECK_GT(max_samples, 1);
63 
64  // For defined behavior, starting with a Tic().
65  Tic();
66  }
67 
68  // Tic and toc. Start and stop loop timer.
69  void Tic();
70  Time Toc();
71 
72  // High probability upper bound on next loop runtime, with initial guess to be
73  // returned if not enough data has been observed yet.
74  Time RuntimeUpperBound(float num_stddevs = 3.0,
75  Time initial_guess = 0.02) const;
76 
77  private:
78  // Maximum number of samples used to compute mean and variance.
79  const size_t max_samples_;
80 
81  // Most recent timer start time.
82  std::chrono::time_point<std::chrono::high_resolution_clock> start_;
83 
84  // Queue of observed loop times.
85  std::list<Time> loop_times_;
86 
87  // Running sum of times in the queue.
88  Time total_time_;
89 }; // class LoopTimer
90 
91 } // namespace ilqgames
92 
93 #endif