time_util.h
Go to the documentation of this file.
1 #include "sick_scan/sick_scan_base.h" /* Base definitions included in all header files, added by add_sick_scan_base_header.py. Do not edit this line. */
2 /*
3  * @brief time_util.h implements utility functions for time measurement and profiling.
4  *
5  * Copyright (C) 2020 Ing.-Buero Dr. Michael Lehning, Hildesheim
6  * Copyright (C) 2020 SICK AG, Waldkirch
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  *
20  * All rights reserved.
21  *
22  * Redistribution and use in source and binary forms, with or without
23  * modification, are permitted provided that the following conditions are met:
24  *
25  * * Redistributions of source code must retain the above copyright
26  * notice, this list of conditions and the following disclaimer.
27  * * Redistributions in binary form must reproduce the above copyright
28  * notice, this list of conditions and the following disclaimer in the
29  * documentation and/or other materials provided with the distribution.
30  * * Neither the name of SICK AG nor the names of its
31  * contributors may be used to endorse or promote products derived from
32  * this software without specific prior written permission
33  * * Neither the name of Ing.-Buero Dr. Michael Lehning nor the names of its
34  * contributors may be used to endorse or promote products derived from
35  * this software without specific prior written permission
36  *
37  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
38  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
39  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
40  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
41  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
42  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
43  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
44  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
45  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
46  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
47  * POSSIBILITY OF SUCH DAMAGE.
48  *
49  * Authors:
50  * Michael Lehning <michael.lehning@lehning.de>
51  *
52  * Copyright 2020 SICK AG
53  * Copyright 2020 Ing.-Buero Dr. Michael Lehning
54  *
55  */
56 #ifndef __SICK_SCANSEGMENT_XD_TIME_UTIL_H
57 #define __SICK_SCANSEGMENT_XD_TIME_UTIL_H
58 
61 
62 namespace sick_scansegment_xd
63 {
64  /*
65  * class TimingStatistics is a utility class to measure basic statistics (mean, max, stddev and histogram) for the duration of a msgpack in milliseconds.
66  */
68  {
69  public:
70 
71  /*
72  * Default constructor, initializes all member with 0
73  */
74  TimingStatistics() : m_cnt(0), m_sum(0), m_sum_sq(0), m_max(0), m_hist(11) {}
75 
76  /*
77  * Adds a duration in milliseconds to the time statistics
78  */
79  void AddTimeMilliseconds(double t)
80  {
81  m_sum += t;
82  m_sum_sq += (t * t);
83  m_max = std::max(m_max, t);
84  int hist_idx = std::min((int)t, (int)m_hist.size() - 1);
85  m_hist[hist_idx] += 1;
86  m_cnt++;
87  }
88 
89  /*
90  * Returns the mean duration in milliseconds
91  */
92  double MeanMilliseconds(void) const
93  {
94  if(m_cnt > 0)
95  {
96  return m_sum / (double)m_cnt;
97  }
98  return 0;
99  }
100 
101  /*
102  * Returns the standard deviation of all durations in milliseconds
103  */
104  double StddevMilliseconds(void) const
105  {
106  if (m_cnt > 1)
107  {
108  double var = (m_sum_sq - (m_sum * m_sum) / m_cnt) / (m_cnt - 1);
109  return std::sqrt(var);
110  }
111  return 0;
112  }
113 
114  /*
115  * Returns the max duration in milliseconds
116  */
117  double MaxMilliseconds(void) const
118  {
119  return m_max;
120  }
121 
122  /*
123  * Prints the duration histogram to string, f.e. "10,5,6,...,3" means 10 durations between 0 and 1 ms, 5 durations between 1 and 2 ms, 3 durations greater 10 ms.
124  */
125  std::string PrintHistMilliseconds(const std::string& separator = ",") const
126  {
127  std::stringstream s;
128  s << m_hist[0];
129  for (size_t n = 1; n < m_hist.size(); n++)
130  s << separator << m_hist[n];
131  return s.str();
132  }
133 
134  protected:
135 
136  size_t m_cnt;
137  double m_sum;
138  double m_sum_sq;
139  double m_max;
140  std::vector<int> m_hist;
141  }; // class TimingStatistics
142 
143 } // namespace sick_scansegment_xd
144 #endif // __SICK_SCANSEGMENT_XD_TIME_UTIL_H
common.h
sick_scansegment_xd::TimingStatistics::m_sum
double m_sum
Definition: time_util.h:137
sick_scansegment_xd::TimingStatistics::MaxMilliseconds
double MaxMilliseconds(void) const
Definition: time_util.h:117
sick_scansegment_xd::TimingStatistics::MeanMilliseconds
double MeanMilliseconds(void) const
Definition: time_util.h:92
sick_scansegment_xd::TimingStatistics::StddevMilliseconds
double StddevMilliseconds(void) const
Definition: time_util.h:104
s
XmlRpcServer s
sick_scansegment_xd
Definition: include/sick_scansegment_xd/common.h:138
sick_scansegment_xd::TimingStatistics::PrintHistMilliseconds
std::string PrintHistMilliseconds(const std::string &separator=",") const
Definition: time_util.h:125
sick_scansegment_xd::TimingStatistics::m_max
double m_max
Definition: time_util.h:139
sick_ros_wrapper.h
sick_scansegment_xd::TimingStatistics::m_cnt
size_t m_cnt
Definition: time_util.h:136
sick_scansegment_xd::TimingStatistics::m_sum_sq
double m_sum_sq
Definition: time_util.h:138
sick_scansegment_xd::TimingStatistics::AddTimeMilliseconds
void AddTimeMilliseconds(double t)
Definition: time_util.h:79
sick_scansegment_xd::TimingStatistics::m_hist
std::vector< int > m_hist
Definition: time_util.h:140
sick_scansegment_xd::TimingStatistics
Definition: time_util.h:67
sick_scan_base.h
t
geometry_msgs::TransformStamped t
sick_scansegment_xd::TimingStatistics::TimingStatistics
TimingStatistics()
Definition: time_util.h:74


sick_scan_xd
Author(s): Michael Lehning , Jochen Sprickerhof , Martin Günther
autogenerated on Fri Oct 25 2024 02:47:12