Borderbasix

Clock.H
Go to the documentation of this file.
1 // -*- C++ -*- class to measure CPU time.
2 // $Id: Clock.H,v 1.1.1.1 2006/10/06 08:01:40 trebuche Exp $
3 //
4 // Written by Jan O. Babst (jan.babst@erlangen.netsurf.de)
5 //
6 
7 #ifndef _Clock_h
8 #define _Clock_h
9 
10 #include <ctime>
11 
12 class Clock
13 {
14 public:
15  // Construct and start clock immediately.
16  //
17  Clock() : start_(clock()), running(true) {};
18 
19  ~Clock() {};
20 
21  // Start clock; same effect as constructor.
22  //
23  void start()
24  {
25  start_ = clock();
26  running = true;
27  }
28 
29  // Time returned by time() is from start of clock
30  // until the call of this function.
31  //
32  void stop()
33  {
34  stop_ = clock();
35  running = false;
36  }
37 
38  // Return elapsed CPU time from start of clock until the call
39  // of stop(), or the current value if clock is running.
40  //
41  double time()
42  {
43  if (running)
44  return (double)(clock() - start_) / CLOCKS_PER_SEC;
45  else
46  return (double)(stop_ - start_) / CLOCKS_PER_SEC;
47  }
48 
49 private:
50  clock_t start_;
51  clock_t stop_;
52  bool running;
53 };
54 
55 #endif // //_Clock_h
Definition: Clock.H:12
void stop()
Definition: Clock.H:32
Clock()
Definition: Clock.H:17
void start()
Definition: Clock.H:23
~Clock()
Definition: Clock.H:19
double time()
Definition: Clock.H:41
Home  |  Download & InstallContributions