TimeMeasure.py
Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 # -*- coding: euc-jp -*-
00003 
00004 ##
00005 # @file TimeMeasure.py
00006 # @brief Periodic time measurement class
00007 # @date $Date$
00008 # @author Noriaki Ando <n-ando@aist.go.jp> and Shinji Kurihara
00009 #
00010 # Copyright (C) 2009
00011 #     Noriaki Ando
00012 #     Task-intelligence Research Group,
00013 #     Intelligent Systems Research Institute,
00014 #     National Institute of
00015 #         Advanced Industrial Science and Technology (AIST), Japan
00016 #     All rights reserved.
00017 #
00018 # $Id$
00019 #
00020 #
00021 
00022 import time
00023 import math
00024 
00025 import OpenRTM_aist
00026 
00027 ULLONG_MAX = 0xffffffffffffffff
00028 
00029 ##
00030 # @if jp
00031 # @brief 時間単位変換用定数
00032 # @else
00033 # @endif
00034 usec_per_sec = 1000000
00035 
00036 ##
00037 # @if jp
00038 # @class Time
00039 # @brief 時間管理用クラス
00040 # 
00041 # 指定した時間値を保持するためのクラス。
00042 # 
00043 # @since 0.4.1
00044 # 
00045 # @else
00046 # 
00047 # @endif
00048 class Time:
00049   """
00050   """
00051 
00052   ##
00053   # @if jp
00054   # @brief コンストラクタ
00055   #
00056   # コンストラクタ。
00057   #
00058   # @param self
00059   #
00060   # @else
00061   # @brief Constructor.
00062   #
00063   # Constructor.
00064   #
00065   # @param self
00066   #
00067   # @endif
00068   def __init__(self):
00069     global usec_per_sec
00070     tm = time.time()
00071     tm_f       = float(tm - long(tm))     # 小数部の取り出し
00072     self.sec   = long(tm - tm_f)   # 整数部の取り出し
00073     self.usec  = long(float(tm_f) * float(usec_per_sec)) # sec -> usec (micro second)
00074     self._timevalue = OpenRTM_aist.TimeValue(self.sec,self.usec)
00075     return
00076 
00077   ##
00078   # @if jp
00079   #
00080   # @brief 時間減算
00081   # 
00082   # 設定された時間から引数で与えられた時間を減算する。
00083   #
00084   # @param self
00085   # @param tm 減算時間
00086   # 
00087   # @return 減算結果
00088   # 
00089   # @else
00090   #
00091   # @endif
00092   def __sub__(self, tm):
00093     global usec_per_sec
00094 
00095     res = Time()
00096     
00097     if self.sec >= tm.sec:
00098       if self.usec >= tm.usec:
00099         res.sec  = self.sec  - tm.sec
00100         res.usec = self.usec - tm.usec
00101       else:
00102         res.sec  = self.sec  - tm.sec - 1
00103         res.usec = (self.usec + usec_per_sec) - tm.usec
00104     else:
00105       if tm.usec >= self.usec:
00106         res.sec  = -(tm.sec  - self.sec)
00107         res.usec = -(tm.usec - self.usec)
00108       else:
00109         res.sec  = -(tm.sec - self.sec - 1)
00110         res.usec = -(tm.usec + usec_per_sec) + self.usec
00111     return res
00112 
00113     
00114   def gettimeofday(self):
00115     global usec_per_sec
00116     tm = time.time()
00117     tm_f       = float(tm - long(tm))     # 小数部の取り出し
00118     self.sec   = long(float(tm) - float(tm_f))   # 整数部の取り出し
00119     self.usec  = long(float(tm_f) * float(usec_per_sec)) # sec -> usec (micro second)
00120     return OpenRTM_aist.TimeValue(self.sec, self.usec)
00121 
00122 
00123   def getTime(self):
00124     return OpenRTM_aist.TimeValue(self.sec, self.usec)
00125 
00126 
00127 ##
00128 # TimeMeasure object
00129 #
00130 # This object is used for getting statistics of code execution time. 
00131 # Using get_stat you can get maximum, minimum, mean and standard
00132 # deviation time for code execution.
00133 #
00134 class TimeMeasure:
00135   """
00136   """
00137 
00138   ##
00139   # @brief Time statictics object for profiling.
00140   # 
00141   # Constructor
00142   #
00143   def __init__(self, buflen=100):
00144     self._countMax = buflen + 1
00145     self._record = [OpenRTM_aist.TimeValue(0, 0) for i in range(self._countMax)]
00146     self._begin  = Time().gettimeofday()
00147     self._end  = Time().gettimeofday()
00148     self._count = 0
00149     self._recurred = False
00150     self._interval = OpenRTM_aist.TimeValue(0.0)
00151     return
00152 
00153   ##
00154   # @brief Begin time measurement for time statistics.
00155   #
00156   # Begin time measurement for time statistics
00157   #
00158   def tick(self):
00159     self._begin = Time().gettimeofday()
00160     return
00161 
00162   ##
00163   # @brief Finish time measurement for time statistics.
00164   #
00165   # End of time measurement for time statistics
00166   #
00167   def tack(self):
00168     if self._begin.sec() == 0:
00169       return
00170 
00171     self._interval = Time().gettimeofday() - self._begin
00172     self._end = Time().gettimeofday()
00173     self._record[self._count] = self._interval
00174     self._count += 1
00175     if self._count == self._countMax:
00176       self._count = 0
00177       self._recurred = True
00178     return
00179 
00180   def interval(self):
00181     return self._interval
00182 
00183 
00184   def reset(self):
00185     self._count = 0
00186     self._recurred = False
00187     self._begin = OpenRTM_aist.TimeValue(0.0)
00188     return
00189     
00190   ##
00191   # Get number of time measurement buffer
00192   #
00193   # @brief Get number of time measurement buffer.
00194   #
00195   #
00196   def count(self):
00197     if self._recurred:
00198       return len(self._record)
00199     else:
00200       return self._count
00201     return
00202     
00203   ##
00204   # @brief Get total statistics.
00205   # Get total statistics
00206   # max_interval, min_interval, mean_interval [ns]
00207   #
00208   def getStatistics(self, max_interval=None, min_interval=None,
00209                     mean_interval=None, stddev=None):
00210     global ULLONG_MAX
00211 
00212     if not max_interval and not min_interval and not mean_interval and not stddev:
00213       max_i  = [0.0]
00214       min_i  = [0.0]
00215       mean_i = [0.0]
00216       stdd   = [0.0]
00217       return self.getStatistics(max_i, min_i, mean_i, stdd)
00218 
00219     max_interval[0] = 0.0
00220     min_interval[0] = ULLONG_MAX
00221 
00222     sum_ = 0.0
00223     sq_sum_ = 0.0
00224     len_ = self.count()
00225         
00226     if len_ == 0:
00227       return False
00228 
00229     for i in range(len_):
00230       trecord_ = self._record[i].toDouble()
00231       sum_ += trecord_
00232       sq_sum_ += (trecord_ * trecord_)
00233       
00234       if trecord_ > max_interval[0]:
00235         max_interval[0] = trecord_
00236             
00237       if trecord_ < min_interval[0]:
00238         min_interval[0] = trecord_
00239             
00240                 
00241     mean_interval[0] = sum_ / len_
00242     stddev[0] = math.sqrt(sq_sum_ / len_ - (mean_interval[0] * mean_interval[0]))
00243 
00244     return True
00245         
00246 
00247   class Statistics:
00248     def __init__(self, max=None, min=None, mean=None, stdd=None):
00249       if not max and not min and not mean and not stdd:
00250         self._max_interval  = 0.0
00251         self._min_interval  = 0.0
00252         self._mean_interval = 0.0
00253         self._std_deviation = 0.0
00254         return
00255 
00256       self._max_interval  = max
00257       self._min_interval  = min
00258       self._mean_interval = mean
00259       self._std_deviation = stdd
00260       return


openrtm_aist_python
Author(s): Shinji Kurihara
autogenerated on Thu Aug 27 2015 14:17:28