Zero  0.1.0
stopwatch.h
Go to the documentation of this file.
1 /* -*- mode:C++; c-basic-offset:4 -*-
2  Shore-kits -- Benchmark implementations for Shore-MT
3 
4  Copyright (c) 2007-2009
5  Data Intensive Applications and Systems Labaratory (DIAS)
6  Ecole Polytechnique Federale de Lausanne
7 
8  All Rights Reserved.
9 
10  Permission to use, copy, modify and distribute this software and
11  its documentation is hereby granted, provided that both the
12  copyright notice and this permission notice appear in all copies of
13  the software, derivative works or modified versions, and any
14  portions thereof, and that both notices appear in supporting
15  documentation.
16 
17  This code is distributed in the hope that it will be useful, but
18  WITHOUT ANY WARRANTY; without even the implied warranty of
19  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. THE AUTHORS
20  DISCLAIM ANY LIABILITY OF ANY KIND FOR ANY DAMAGES WHATSOEVER
21  RESULTING FROM THE USE OF THIS SOFTWARE.
22 */
23 
24 #ifndef __STOPWATCH_H
25 #define __STOPWATCH_H
26 
27 #include <sys/time.h>
28 
32 class stopwatch_t {
33 private:
34  struct timeval tv;
35 
36  long long mark;
37 
38 public:
40  reset();
41  }
42 
43  long long time_us() {
44  long long old_mark = mark;
45  reset();
46  return mark - old_mark;
47  }
48 
49  double time_ms() {
50  return ((double)(time_us() * 1e-3));
51  }
52 
53  double time() {
54  return ((double)(time_us() * 1e-6));
55  }
56 
57  long long now() {
58  gettimeofday(&tv, nullptr);
59  return tv.tv_usec + tv.tv_sec * 1000000ll;
60  }
61 
62  void reset() {
63  mark = now();
64  }
65 };
66 
67 #endif // __STOPWATCH_H
stopwatch_t()
Definition: stopwatch.h:39
long long time_us()
Definition: stopwatch.h:43
long long now()
Definition: stopwatch.h:57
double time_ms()
Definition: stopwatch.h:49
double time()
Definition: stopwatch.h:53
a timer object.
Definition: stopwatch.h:34
long long mark
Definition: stopwatch.h:37
void reset()
Definition: stopwatch.h:57
struct timeval tv
Definition: stopwatch.h:36