ARB
perf_timer.h
Go to the documentation of this file.
1 // ==================================================================== //
2 // //
3 // File : perf_timer.h //
4 // Purpose : Simple performance timer using clock() //
5 // //
6 // //
7 // Coded by Ralf Westram (coder@reallysoft.de) in November 2004 //
8 // Copyright Department of Microbiology (Technical University Munich) //
9 // //
10 // Visit our web site at: http://www.arb-home.de/ //
11 // //
12 // ==================================================================== //
13 #ifndef PERF_TIMER_H
14 #define PERF_TIMER_H
15 
16 #ifndef _GLIBCXX_STRING
17 #include <string>
18 #endif
19 #ifndef _GLIBCXX_CSTDIO
20 #include <cstdio>
21 #endif
22 #ifndef _GLIBCXX_CTIME
23 #include <ctime>
24 #endif
25 
26 class PerfTimer {
27  clock_t started_at;
28  unsigned long loop_counter;
29  std::string message;
30 
31 public:
32 
33  PerfTimer(const std::string& message_) :
34  started_at(clock()),
35  loop_counter(0),
36  message(message_)
37  {}
38 
40  clock_t stopped_at = clock();
41  clock_t ticks = stopped_at-started_at;
42  double seconds = double(ticks)/CLOCKS_PER_SEC;
43 
44  printf("Time for '%s': ticks=%lu (= %5.2f seconds)",
45  message.c_str(), ticks, seconds);
46 
47  if (loop_counter > 0) { // loop timer
48  if (loop_counter == 1) {
49  printf(" 1 loop");
50  }
51  else {
52  double lticks = double(ticks)/loop_counter;
53  double lseconds = lticks/CLOCKS_PER_SEC;
54 
55  printf(" %lu loops. Each took: ticks=%lu",
56  loop_counter, (clock_t)(lticks+0.5));
57  if (lseconds >= 0.01) {
58  printf(" (= %5.2f seconds)", lseconds);
59  }
60  else {
61  printf(" (= %5.2f milliseconds)", lseconds/1000);
62  }
63 
64  double loopsPerSecond = loop_counter/seconds;
65  printf(" = %5.2f loops/second", loopsPerSecond);
66  }
67  }
68  printf("\n");
69  }
70 
71  void announceLoop() { loop_counter++; }
72 };
73 
74 
75 
76 #else
77 #error perf_timer.h included twice
78 #endif // PERF_TIMER_H
79 
return string(buffer, length)
PerfTimer(const std::string &message_)
Definition: perf_timer.h:33
~PerfTimer()
Definition: perf_timer.h:39
void announceLoop()
Definition: perf_timer.h:71