OpenANN  1.1.0
An open source library for artificial neural networks.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Stopwatch.h
Go to the documentation of this file.
1 #ifndef OPENANN_UTIL_STOPWATCH_H_
2 #define OPENANN_UTIL_STOPWATCH_H_
3 
4 #include <sys/time.h>
5 
6 class Stopwatch
7 {
8  unsigned long begin, duration;
9 
11  inline unsigned long getSystime()
12  {
13  timeval t;
14  gettimeofday(&t,0);
15  return (unsigned long) t.tv_sec * 1000000L + (unsigned long) t.tv_usec;
16  }
17 public:
19 
21  Stopwatch() : begin(getSystime()), duration(0) {};
22 
24  void start()
25  {
26  begin = getSystime();
27  }
28 
34  inline unsigned long stop()
35  {
36  duration = getSystime() - begin;
37  return duration;
38  }
39 
45  inline unsigned long stop(Precision p)
46  {
47  unsigned long duration = stop();
48  switch(p)
49  {
50  case SECONDS:
51  return duration / 1000000L;
52  case TENTHS:
53  return duration / 100000L;
54  case HUNDREDTHS:
55  return duration / 10000L;
56  case MILLISECOND:
57  return duration / 1000L;
58  case MICROSECOND:
59  default:
60  return duration;
61  }
62  }
63 
68  inline unsigned long getDuration()
69  {
70  return duration;
71  }
72 };
73 
74 #endif // OPENANN_UTIL_STOPWATCH_H_
75