[go: up one dir, main page]

Menu

[5a71d8]: / common / timer.h  Maximize  Restore  History

Download this file

85 lines (67 with data), 1.2 kB

 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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#ifndef TIMER_HEADER
#define TIMER_HEADER
/* Simple timer class by Roman Dementiev
*/
#include "../common/utils.h"
#include <time.h>
#include <sys/time.h>
__STXXL_BEGIN_NAMESPACE
class timer
{
bool running;
double accumulated;
double last_clock;
double timestamp();
public:
timer();
void start();
void stop();
void reset();
double seconds();
double mseconds();
double useconds();
};
timer::timer():running(false),accumulated(0.)
{
}
double timer::timestamp()
{
struct timeval tp;
gettimeofday(&tp, NULL);
return double(tp.tv_sec) + tp.tv_usec / 1000000.;
}
void timer::start()
{
running = true;
last_clock = timestamp();
}
void timer::stop()
{
running = false;
accumulated += timestamp() - last_clock;
}
void timer::reset()
{
accumulated = 0.;
last_clock = timestamp();
}
double timer::mseconds()
{
if(running)
return (accumulated + timestamp() - last_clock)*1000.;
return (accumulated*1000.);
}
double timer::useconds()
{
if(running)
return (accumulated + timestamp() - last_clock)*1000000.;
return (accumulated*1000000.);
}
double timer::seconds()
{
if(running)
return (accumulated + timestamp() - last_clock);
return (accumulated);
}
__STXXL_END_NAMESPACE
#endif