TimeMeasure.cpp
Go to the documentation of this file.
00001 // -*- C++ -*-
00020 #include <coil/Time.h>
00021 #include <math.h>
00022 #include <coil/TimeMeasure.h>
00023 
00024 //#define RDTSC(X) __asm__ __volatile__ ("rdtsc" : "=A" (X))
00025 //#define NSEC_PER_SEC 1000000000
00026 
00027 #ifndef ULLONG_MAX
00028 #define ULLONG_MAX 0xffffffffffffffffULL
00029 #endif 
00030 
00031 namespace coil
00032 {
00033 
00041   TimeMeasure::TimeMeasure(int buflen)
00042     : m_begin(0.0), m_interval(0.0),
00043       m_count(0), m_countMax(buflen + 1),
00044       m_recurred(false)
00045   {
00046     m_record.reserve(m_countMax);
00047     for (unsigned long i(0); i < m_countMax; ++i)
00048       {
00049         m_record.push_back(TimeValue(0, 0));
00050       }
00051   }
00052 
00060   void TimeMeasure::tick()
00061   {
00062     m_begin = gettimeofday(); // [TimeValue]
00063   }
00064 
00072   void TimeMeasure::tack()
00073   {
00074     if (m_begin.sec() == 0) { return; }
00075 
00076     m_interval = gettimeofday() - m_begin;
00077     m_record.at(m_count) = m_interval;
00078     ++m_count;
00079     if (m_count == m_countMax)
00080       {
00081         m_count = 0;
00082         m_recurred = true;
00083       }
00084   }
00085 
00093   coil::TimeValue& TimeMeasure::interval()
00094   {
00095     return m_interval;
00096   }
00097 
00105   void TimeMeasure::reset()
00106   {
00107     m_count = 0;
00108     m_recurred = false;
00109     m_begin = 0.0;
00110   }
00111 
00119   unsigned long int TimeMeasure::count() const
00120   {
00121     return m_recurred ? m_record.size() : m_count;
00122   }
00123 
00131   bool TimeMeasure::getStatistics(double &max_interval,
00132                                   double &min_interval,
00133                                   double &mean_interval,
00134                                   double &stddev)
00135   {
00136     max_interval = (double)0;
00137     min_interval = (double)ULLONG_MAX;
00138 
00139     double sum = 0;
00140     double sq_sum = 0;
00141     unsigned long int len(count());
00142 
00143     if (len == 0) return false;
00144 
00145     for (unsigned long int i(0); i < len; ++i)
00146       {
00147         double trecord(m_record[i]);
00148         sum += trecord;
00149         sq_sum += trecord * trecord;
00150         
00151         if (trecord > max_interval) max_interval = trecord;
00152         if (trecord < min_interval) min_interval = trecord;
00153       }
00154     
00155     mean_interval = sum / len;
00156     stddev = sqrt(sq_sum / len - (mean_interval * mean_interval));
00157 
00158     return true;
00159   }
00160 
00168   TimeMeasure::Statistics TimeMeasure::getStatistics()
00169   {
00170     Statistics s;
00171     getStatistics(s.max_interval, s.min_interval,
00172                   s.mean_interval, s.std_deviation);
00173     return s;
00174   }
00175 
00176 }; // namespace coil


openrtm_aist
Author(s): Noriaki Ando
autogenerated on Thu Aug 27 2015 14:16:39