• Main Page
  • Namespaces
  • Classes
  • Files
  • File List
  • File Members

ibrcommon/ibrcommon/TimeMeasurement.cpp

Go to the documentation of this file.
00001 /*
00002  * TimeMeasurement.cpp
00003  *
00004  *  Created on: 20.01.2010
00005  *      Author: morgenro
00006  */
00007 
00008 #include "ibrcommon/TimeMeasurement.h"
00009 #include <iostream>
00010 #include <iomanip>
00011 #include <features.h>
00012 #include <stdio.h>
00013 
00014 namespace ibrcommon
00015 {
00016         TimeMeasurement::TimeMeasurement()
00017         {
00018                 start(); stop();
00019         }
00020 
00021         TimeMeasurement::~TimeMeasurement()
00022         {
00023         }
00024 
00025         void TimeMeasurement::start()
00026         {
00027                 // set sending time
00028                 clock_gettime(CLOCK_MONOTONIC, &_start);
00029         }
00030 
00031         void TimeMeasurement::stop()
00032         {
00033                 //We are DOOOOMED
00034                 //http://www.wand.net.nz/~smr26/wordpress/2009/01/19/monotonic-time-in-mac-os-x/
00035                 // set receiving time
00036                 clock_gettime(CLOCK_MONOTONIC, &_end);
00037         }
00038 
00039         float TimeMeasurement::getMilliseconds()
00040         {
00041                 // calc difference
00042                 u_int64_t timeElapsed = TimeMeasurement::timespecDiff(&_end, &_start);
00043 
00044                 // make it readable
00045                 float delay_ms = (float)timeElapsed / 1000000;
00046 
00047                 return delay_ms;
00048         }
00049 
00050         float TimeMeasurement::getSeconds()
00051         {
00052                 return getMilliseconds() / 1000;
00053         }
00054 
00055         std::ostream& TimeMeasurement::format(std::ostream &stream, const float value)
00056         {
00057 #ifdef __UCLIBC__
00058                 char buf[32];
00059                 snprintf(buf, 32, "%4.2f", value);
00060                 stream << std::string(buf);
00061 #else
00062                 stream << std::setiosflags(std::ios::fixed) << std::setprecision(2) << value;
00063 #endif
00064                 return stream;
00065         }
00066 
00067         std::ostream &operator<<(std::ostream &stream, TimeMeasurement &measurement)
00068         {
00069                 // calc difference
00070                 u_int64_t timeElapsed = TimeMeasurement::timespecDiff(&(measurement._end), &(measurement._start));
00071 
00072                 // make it readable
00073                 float delay_ms = (float)timeElapsed / 1000000;
00074                 float delay_sec = delay_ms / 1000;
00075                 float delay_min = delay_sec / 60;
00076                 float delay_h = delay_min / 60;
00077 
00078                 if (delay_h > 1)
00079                 {
00080                         TimeMeasurement::format(stream, delay_h); stream << " h";
00081                 }
00082                 else if (delay_min > 1)
00083                 {
00084                         TimeMeasurement::format(stream, delay_min); stream << " m";
00085                 }
00086                 else if (delay_sec > 1)
00087                 {
00088                         TimeMeasurement::format(stream, delay_sec); stream << " s";
00089                 }
00090                 else
00091                 {
00092                         TimeMeasurement::format(stream, delay_ms); stream << " ms";
00093                 }
00094 
00095                 return stream;
00096         }
00097 
00098         int64_t TimeMeasurement::timespecDiff(struct timespec *timeA_p, struct timespec *timeB_p)
00099         {
00100                 //Casting to 64Bit, otherwise it caps out at ~ 5 secs for 32bit machines
00101                return (   (  (int64_t)(timeA_p->tv_sec) * 1e9 + (int64_t)(timeA_p->tv_nsec)) -  ( (int64_t)(timeB_p->tv_sec) * 1e9 + (int64_t)(timeB_p->tv_nsec)) );
00102         }
00103 }

Generated on Wed Mar 30 2011 11:11:49 for IBR-DTNSuite by  doxygen 1.7.1