File: perf_timer.h

package info (click to toggle)
arb 6.0.6-8
  • links: PTS, VCS
  • area: non-free
  • in suites: sid, trixie
  • size: 66,204 kB
  • sloc: ansic: 394,911; cpp: 250,290; makefile: 19,644; sh: 15,879; perl: 10,473; fortran: 6,019; ruby: 683; xml: 503; python: 53; awk: 32
file content (79 lines) | stat: -rw-r--r-- 2,548 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
//  ==================================================================== //
//                                                                       //
//    File      : perf_timer.h                                           //
//    Purpose   : Simple performance timer using clock()                 //
//                                                                       //
//                                                                       //
//  Coded by Ralf Westram (coder@reallysoft.de) in November 2004         //
//  Copyright Department of Microbiology (Technical University Munich)   //
//                                                                       //
//  Visit our web site at: http://www.arb-home.de/                       //
//                                                                       //
//  ==================================================================== //
#ifndef PERF_TIMER_H
#define PERF_TIMER_H

#ifndef _GLIBCXX_STRING
#include <string>
#endif
#ifndef _GLIBCXX_CSTDIO
#include <cstdio>
#endif
#ifndef _GLIBCXX_CTIME
#include <ctime>
#endif

class PerfTimer {
    clock_t       started_at;
    unsigned long loop_counter;
    std::string   message;

public:

    PerfTimer(const std::string& message_)
        : started_at(clock())
        , loop_counter(0)
        , message(message_)
    {}

    ~PerfTimer() {
        clock_t stopped_at = clock();
        clock_t ticks      = stopped_at-started_at;
        double  seconds    = double(ticks)/CLOCKS_PER_SEC;

        printf("Time for '%s': ticks=%lu (= %5.2f seconds)",
               message.c_str(), ticks, seconds);

        if (loop_counter > 0) { // loop timer
            if (loop_counter == 1) {
                printf(" 1 loop");
            }
            else {
                double lticks  = double(ticks)/loop_counter;
                double lseconds = lticks/CLOCKS_PER_SEC;

                printf(" %lu loops. Each took: ticks=%lu",
                       loop_counter, (clock_t)(lticks+0.5));
                if (lseconds >= 0.01) {
                    printf(" (= %5.2f seconds)", lseconds);
                }
                else {
                    printf(" (= %5.2f milliseconds)", lseconds/1000);
                }

                double loopsPerSecond = loop_counter/seconds;
                printf(" = %5.2f loops/second", loopsPerSecond);
            }
        }
        printf("\n");
    }

    void announceLoop() { loop_counter++; }
};



#else
#error perf_timer.h included twice
#endif // PERF_TIMER_H