]> git.lyx.org Git - lyx.git/blob - src/support/pmprof.h
f1506ec222485f9c3aed8287b7b2a40c21113cb3
[lyx.git] / src / support / pmprof.h
1 // -*- C++ -*-
2 /* \file pmprof.h
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Jean-Marc Lasgouttes
7  *
8  * Full author contact details are available in file CREDITS.
9  */
10
11 #ifndef PMPROF_H
12 #define PMPROF_H
13
14 #include <sys/time.h>
15 #include <iostream>
16
17 /** How to use this trivial profiler:
18  *
19  * * at the beginning of the interesting block, just add:
20  *   PROFILE_THIS_BLOCK(some_identifier);
21  *
22  * * when the program ends, statistics will be sent to standard error, like:
23  *
24  *   ##### some_identifier: 6.48475usec, count=25405
25  *
26  * The code measured by the profiler corresponds to the lifetime of a
27  * local variable declared by the PROFILE_THIS_BLOCK macro.
28  */
29
30 /* Helper class for gathering data. Instantiate this as a static
31  * variable, so that its destructor will be executed when the program
32  * ends.
33  */
34 class PMProfStat {
35 public:
36         PMProfStat(char const * name)
37           : name_(name), sec_(0), usec_(0), count_(0) {};
38
39         ~PMProfStat() {
40                 if (count_>0)
41                   std::cerr << "##### " << name_ << ": "
42                             << 1.0 * (sec_ * 1000000 + usec_)/ count_
43                             << "usec, count=" << count_ << std::endl;
44         }
45
46         void add(const long long s, const long long u) {
47                 sec_ += s;
48                 usec_ += u;
49                 count_++;
50         }
51
52 private:
53         char const * name_;
54         long long sec_, usec_;
55         unsigned long long count_;
56 };
57
58
59 /* Helper class which gathers data at the end of the scope. One
60  * instance of this one should be created at each execution of the
61  * block. At the end of the block, it sends statistics to the static
62  * PMProfStat object.
63  */
64 class PMProfInstance {
65 public:
66         PMProfInstance(PMProfStat * stat) : stat_(stat)
67         {
68                 gettimeofday(&before_, 0);
69         }
70
71         ~PMProfInstance() {
72                 gettimeofday(&after_, 0);
73                 stat_->add(after_.tv_sec - before_.tv_sec,
74                            after_.tv_usec - before_.tv_usec);
75         }
76
77 private:
78         timeval before_, after_;
79         PMProfStat * stat_;
80 };
81
82
83 #define PROFILE_THIS_BLOCK(a) \
84         static PMProfStat PMPS_##a(#a);\
85         PMProfInstance PMPI_##a(&PMPS_##a)
86
87
88 #endif