Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008 #include "ibrcommon/TimeMeasurement.h"
00009 #include <iostream>
00010 #include <iomanip>
00011
00012 namespace ibrcommon
00013 {
00014 TimeMeasurement::TimeMeasurement()
00015 {
00016 start(); stop();
00017 }
00018
00019 TimeMeasurement::~TimeMeasurement()
00020 {
00021 }
00022
00023 void TimeMeasurement::start()
00024 {
00025
00026 clock_gettime(CLOCK_MONOTONIC, &_start);
00027 }
00028
00029 void TimeMeasurement::stop()
00030 {
00031
00032
00033
00034 clock_gettime(CLOCK_MONOTONIC, &_end);
00035 }
00036
00037 float TimeMeasurement::getMilliseconds()
00038 {
00039
00040 u_int64_t timeElapsed = TimeMeasurement::timespecDiff(&_end, &_start);
00041
00042
00043 float delay_ms = (float)timeElapsed / 1000000;
00044
00045 return delay_ms;
00046 }
00047
00048 float TimeMeasurement::getSeconds()
00049 {
00050 return getMilliseconds() / 1000;
00051 }
00052
00053 std::ostream &operator<<(std::ostream &stream, TimeMeasurement &measurement)
00054 {
00055
00056 u_int64_t timeElapsed = TimeMeasurement::timespecDiff(&(measurement._end), &(measurement._start));
00057
00058
00059 float delay_ms = (float)timeElapsed / 1000000;
00060 float delay_sec = delay_ms / 1000;
00061 float delay_min = delay_sec / 60;
00062 float delay_h = delay_min / 60;
00063
00064 if (delay_h > 1)
00065 {
00066 stream << std::setiosflags(std::ios::fixed) << std::setprecision(2) << delay_h << " h";
00067 }
00068 else if (delay_min > 1)
00069 {
00070 stream << std::setiosflags(std::ios::fixed) << std::setprecision(2) << delay_min << " m";
00071 }
00072 else if (delay_sec > 1)
00073 {
00074 stream << std::setiosflags(std::ios::fixed) << std::setprecision(2) << delay_sec << " s.";
00075 }
00076 else
00077 {
00078 stream << std::setiosflags(std::ios::fixed) << std::setprecision(2) << delay_ms << " ms";
00079 }
00080
00081 return stream;
00082 }
00083
00084 int64_t TimeMeasurement::timespecDiff(struct timespec *timeA_p, struct timespec *timeB_p)
00085 {
00086
00087 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)) );
00088 }
00089 }