-
Notifications
You must be signed in to change notification settings - Fork 0
/
Timer.h
29 lines (23 loc) · 724 Bytes
/
Timer.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
/******************************************************************************
* timer.h
* record the elapsed time in microseconds (10^{-6} second)
*****************************************************************************/
#ifndef _TIMER_LJ_
#define _TIMER_LJ_
#include <cstdlib>
#include <sys/time.h>
class Timer {
public:
Timer() { m_start = timestamp(); }
void restart() { m_start = timestamp(); }
long long elapsed() { return timestamp() - m_start; }
private:
long long m_start;
// Returns a timestamp ('now') in microseconds
long long timestamp() {
struct timeval tp;
gettimeofday(&tp, nullptr);
return ((long long)(tp.tv_sec))*1000000 + tp.tv_usec;
}
};
#endif