source: tags/cvs_2_svn/TEMPLATES/perf_timer.h

Last change on this file was 3396, checked in by westram, 19 years ago
  • simple class for performance measuring
  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 2.6 KB
Line 
1//  ==================================================================== //
2//                                                                       //
3//    File      : perf_timer.h                                           //
4//    Purpose   : Simple performance timer using clock()                 //
5//    Time-stamp: <Thu Nov/18/2004 13:55 MET Coder@ReallySoft.de>        //
6//                                                                       //
7//                                                                       //
8//  Coded by Ralf Westram (coder@reallysoft.de) in November 2004         //
9//  Copyright Department of Microbiology (Technical University Munich)   //
10//                                                                       //
11//  Visit our web site at: http://www.arb-home.de/                       //
12//                                                                       //
13//  ==================================================================== //
14#ifndef PERF_TIMER_H
15#define PERF_TIMER_H
16
17#ifndef _CPP_STRING
18#include <string>
19#endif
20#ifndef _CPP_CSTDIO
21#include <cstdio>
22#endif
23#ifndef _CPP_CTIME
24#include <ctime>
25#endif
26
27class PerfTimer {
28    clock_t       started_at;
29    unsigned long loop_counter;
30    std::string   message;
31
32public:
33
34    PerfTimer(const std::string& message_)
35        : started_at(clock())
36        , loop_counter(0)
37        , message(message_)
38    {}
39
40    ~PerfTimer() {
41        clock_t stopped_at = clock();
42        clock_t ticks      = stopped_at-started_at;
43        double  seconds    = double(ticks)/CLOCKS_PER_SEC;
44
45        printf("Time for '%s': ticks=%lu (= %5.2f seconds)",
46               message.c_str(), ticks, seconds);
47
48        if (loop_counter > 0) { // loop timer
49            if (loop_counter == 1) {
50                printf(" 1 loop");
51            }
52            else {
53                double lticks  = double(ticks)/loop_counter;
54                double lseconds = lticks/CLOCKS_PER_SEC;
55
56                printf(" %lu loops. Each took: ticks=%lu",
57                       loop_counter, (clock_t)(lticks+0.5));
58                if (lseconds >= 0.01) {
59                    printf(" (= %5.2f seconds)", lseconds);
60                }
61                else {
62                    printf(" (= %5.2f milliseconds)", lseconds/1000);
63                }
64
65                double loopsPerSecond = loop_counter/seconds;
66                printf(" = %5.2f loops/second", loopsPerSecond);
67            }
68        }
69        printf("\n");
70    }
71
72    void announceLoop() { loop_counter++; }
73};
74
75
76
77#else
78#error perf_timer.h included twice
79#endif // PERF_TIMER_H
80
Note: See TracBrowser for help on using the repository browser.