00001 #ifndef COMMONS_TIME_H
00002 #define COMMONS_TIME_H
00003
00004 #include <string>
00005 #include <iostream>
00006
00007 #include <sys/time.h>
00008 #include <time.h>
00009
00010 namespace commons
00011 {
00012
00013 using namespace std;
00014
00019 inline long long
00020 current_time_millis()
00021 {
00022 long long t;
00023 struct timeval tv;
00024
00025 gettimeofday(&tv, 0);
00026
00027 t = tv.tv_sec;
00028 t = (t *1000) + (tv.tv_usec/1000);
00029
00030 return t;
00031 }
00032
00036 class timer
00037 {
00038 public:
00039 timer(const string label) :
00040 label(label), start(current_time_millis()), last(start) {}
00041 void print()
00042 {
00043 long long now = current_time_millis();
00044 cout << label << now - last << endl;
00045 last = now;
00046 }
00047 private:
00048 const string label;
00049 long long start, last;
00050 };
00051
00052 }
00053
00054 #endif