TimeMeasure.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 # -*- coding: euc-jp -*-
3 
4 
21 
22 import time
23 import math
24 
25 import OpenRTM_aist
26 
27 ULLONG_MAX = 0xffffffffffffffff
28 
29 
34 usec_per_sec = 1000000
35 
36 
48 class Time:
49  """
50  """
51 
52 
68  def __init__(self):
69  global usec_per_sec
70  tm = time.time()
71  tm_f = float(tm - long(tm)) # 小数部の取り出し
72  self.sec = long(tm - tm_f) # 整数部の取り出し
73  self.usec = long(float(tm_f) * float(usec_per_sec)) # sec -> usec (micro second)
75  return
76 
77 
92  def __sub__(self, tm):
93  global usec_per_sec
94 
95  res = Time()
96 
97  if self.sec >= tm.sec:
98  if self.usec >= tm.usec:
99  res.sec = self.sec - tm.sec
100  res.usec = self.usec - tm.usec
101  else:
102  res.sec = self.sec - tm.sec - 1
103  res.usec = (self.usec + usec_per_sec) - tm.usec
104  else:
105  if tm.usec >= self.usec:
106  res.sec = -(tm.sec - self.sec)
107  res.usec = -(tm.usec - self.usec)
108  else:
109  res.sec = -(tm.sec - self.sec - 1)
110  res.usec = -(tm.usec + usec_per_sec) + self.usec
111  return res
112 
113 
114  def gettimeofday(self):
115  global usec_per_sec
116  tm = time.time()
117  tm_f = float(tm - long(tm)) # 小数部の取り出し
118  self.sec = long(float(tm) - float(tm_f)) # 整数部の取り出し
119  self.usec = long(float(tm_f) * float(usec_per_sec)) # sec -> usec (micro second)
120  return OpenRTM_aist.TimeValue(self.sec, self.usec)
121 
122 
123  def getTime(self):
124  return OpenRTM_aist.TimeValue(self.sec, self.usec)
125 
126 
127 
135  """
136  """
137 
138 
143  def __init__(self, buflen=100):
144  self._countMax = buflen + 1
145  self._record = [OpenRTM_aist.TimeValue(0, 0) for i in range(self._countMax)]
146  self._begin = Time().gettimeofday()
147  self._end = Time().gettimeofday()
148  self._count = 0
149  self._recurred = False
151  return
152 
153 
158  def tick(self):
159  self._begin = Time().gettimeofday()
160  return
161 
162 
167  def tack(self):
168  if self._begin.sec() == 0:
169  return
170 
171  self._interval = Time().gettimeofday() - self._begin
172  self._end = Time().gettimeofday()
173  self._record[self._count] = self._interval
174  self._count += 1
175  if self._count == self._countMax:
176  self._count = 0
177  self._recurred = True
178  return
179 
180  def interval(self):
181  return self._interval
182 
183 
184  def reset(self):
185  self._count = 0
186  self._recurred = False
187  self._begin = OpenRTM_aist.TimeValue(0.0)
188  return
189 
190 
196  def count(self):
197  if self._recurred:
198  return len(self._record)
199  else:
200  return self._count
201  return
202 
203 
208  def getStatistics(self, max_interval=None, min_interval=None,
209  mean_interval=None, stddev=None):
210  global ULLONG_MAX
211 
212  if not max_interval and not min_interval and not mean_interval and not stddev:
213  max_i = [0.0]
214  min_i = [0.0]
215  mean_i = [0.0]
216  stdd = [0.0]
217  return self.getStatistics(max_i, min_i, mean_i, stdd)
218 
219  max_interval[0] = 0.0
220  min_interval[0] = ULLONG_MAX
221 
222  sum_ = 0.0
223  sq_sum_ = 0.0
224  len_ = self.count()
225 
226  if len_ == 0:
227  return False
228 
229  for i in range(len_):
230  trecord_ = self._record[i].toDouble()
231  sum_ += trecord_
232  sq_sum_ += (trecord_ * trecord_)
233 
234  if trecord_ > max_interval[0]:
235  max_interval[0] = trecord_
236 
237  if trecord_ < min_interval[0]:
238  min_interval[0] = trecord_
239 
240 
241  mean_interval[0] = sum_ / len_
242  stddev[0] = math.sqrt(sq_sum_ / len_ - (mean_interval[0] * mean_interval[0]))
243 
244  return True
245 
246 
247  class Statistics:
248  def __init__(self, max=None, min=None, mean=None, stdd=None):
249  if not max and not min and not mean and not stdd:
250  self._max_interval = 0.0
251  self._min_interval = 0.0
252  self._mean_interval = 0.0
253  self._std_deviation = 0.0
254  return
255 
256  self._max_interval = max
257  self._min_interval = min
258  self._mean_interval = mean
259  self._std_deviation = stdd
260  return
def getStatistics(self, max_interval=None, min_interval=None, mean_interval=None, stddev=None)
Get total statistics.
Definition: TimeMeasure.py:209
def __init__(self)
Constructor.
Definition: TimeMeasure.py:68
def tick(self)
Begin time measurement for time statistics.
Definition: TimeMeasure.py:158
def __init__(self, max=None, min=None, mean=None, stdd=None)
Definition: TimeMeasure.py:248
def count(self)
Get number of time measurement buffer.
Definition: TimeMeasure.py:196
def tack(self)
Finish time measurement for time statistics.
Definition: TimeMeasure.py:167
def __init__(self, buflen=100)
Time statictics object for profiling.
Definition: TimeMeasure.py:143


openrtm_aist_python
Author(s): Shinji Kurihara
autogenerated on Mon Feb 28 2022 23:01:07