#ifndef __MICRO_TIME_H__ #define __MICRO_TIME_H__ #include class Time { private: struct timeval t; public: Time() { struct timezone junk; gettimeofday(&t, &junk); } /*************************************** * Updates the current time and returns * the number of microseconds elapsed * since the last update. * Warning if the time difference is REALLY HUGE! (A few years!) * this will be incorrect! * Should throw an exception - but doesn't! ***************************************/ long update() { Time newTime; long dus = newTime - *this; t = newTime.t; return dus; } /*************************************** * Returns t - u in microseconds (millionths) * Warning if the time difference is HUGE! * this will be incorrect! * Should throw an exception - but doesn't! ***************************************/ friend long operator-(const Time& t, const Time& u) { long ds = t.t.tv_sec - u.t.tv_sec; long dus = t.t.tv_usec - u.t.tv_usec; return ds * 1000000 + dus; } friend ostream& operator<<(ostream& out, const Time& t) { double e = t.t.tv_sec % 10 + ((double) t.t.tv_usec)/1000000; return out << t.t.tv_sec/10 << e; } }; #endif