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, mean_interval=None, stddev=None):
209  global ULLONG_MAX
210 
211  if not max_interval and not min_interval and not mean_interval and not stddev:
212  max_i = [0.0]
213  min_i = [0.0]
214  mean_i = [0.0]
215  stdd = [0.0]
216  return self.getStatistics(max_i, min_i, mean_i, stdd)
217 
218  max_interval[0] = 0.0
219  min_interval[0] = ULLONG_MAX
220 
221  sum_ = 0.0
222  sq_sum_ = 0.0
223  len_ = self.count()
224 
225  if len_ == 0:
226  return False
227 
228  for i in range(len_):
229  trecord_ = self._record[i].toDouble()
230  sum_ += trecord_
231  sq_sum_ += (trecord_ * trecord_)
232 
233  if trecord_ > max_interval[0]:
234  max_interval[0] = trecord_
235 
236  if trecord_ < min_interval[0]:
237  min_interval[0] = trecord_
238 
239 
240  mean_interval[0] = sum_ / len_
241  stddev[0] = math.sqrt(sq_sum_ / len_ - (mean_interval[0] * mean_interval[0]))
242 
243  return True
244 
245 
246  class Statistics:
247  def __init__(self, max=None, min=None, mean=None, stdd=None):
248  if not max and not min and not mean and not stdd:
249  self._max_interval = 0.0
250  self._min_interval = 0.0
251  self._mean_interval = 0.0
252  self._std_deviation = 0.0
253  return
254 
255  self._max_interval = max
256  self._min_interval = min
257  self._mean_interval = mean
258  self._std_deviation = stdd
259  return
260 
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 Thu Jun 6 2019 19:11:35