+
+static struct timeval _timer = {0, 0l};
+
+static struct timeval timer_diff(struct timeval a, struct timeval b)
+{
+ struct timeval tv;
+ if ((b.tv_usec - a.tv_usec) < 0) {
+ tv.tv_sec = b.tv_sec - a.tv_sec - 1;
+ tv.tv_usec = 1000000l + b.tv_usec - a.tv_usec;
+ } else {
+ tv.tv_sec = b.tv_sec - a.tv_sec;
+ tv.tv_usec = b.tv_usec - a.tv_usec;
+ }
+ return tv;
+}
+
+void timer_start()
+{
+ int result = gettimeofday(&_timer, NULL);
+ assert(result == 0 && "umm... gettimeofday failed!?");
+}
+
+long timer_stop()
+{
+ struct timeval tv;
+ int result = gettimeofday(&tv, NULL);
+ assert(result == 0 && "umm... gettimeofday failed!?");
+ tv = timer_diff(_timer, tv);
+ return (long)tv.tv_sec * 1000000l + tv.tv_usec;
+}
+