3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Jean-Marc Lasgouttes
8 * Full author contact details are available in file CREDITS.
17 /** How to use this trivial profiler:
19 * * at the beginning of the interesting block, just add:
20 * PROFILE_THIS_BLOCK(some_identifier)
22 * A trailing semicolon can be added at your discretion.
24 * * when the program ends, statistics will be sent to standard error, like:
26 * ##### some_identifier: 6.48475usec, count=25405
28 * The code measured by the profiler corresponds to the lifetime of a
29 * local variable declared by the PROFILE_THIS_BLOCK macro.
31 * Some examples of profiling scope: In the snippets below, c1, c2...
32 * designate code chunks, and the identifiers of profiling blocks are
33 * chosen to reflect what they count.
37 * PROFILE_THIS_BLOCK(c2)
43 * PROFILE_THIS_BLOCK(c1_c2)
45 * PROFILE_THIS_BLOCK(c2)
52 * PROFILE_THIS_BLOCK(c1)
55 * PROFILE_THIS_BLOCK(c2)
61 * PROFILE_THIS_BLOCK(c1_c2_c3)
64 * PROFILE_THIS_BLOCK(c2)
70 * Influence of identifier names: they are mainly used for display
71 * purpose, but the same name should not be used twice in the same
75 * PROFILE_THIS_BLOCK(foo)
77 * PROFILE_THIS_BLOCK(foo) // error: identifier clash
81 * In the example below, c1+c2 and c2 are counted separately, but in
82 * the output, both are confusingly labelled `foo'.
85 * PROFILE_THIS_BLOCK(foo)
88 * PROFILE_THIS_BLOCK(foo) // error: identifier clash
96 /* Helper class for gathering data. Instantiate this as a static
97 * variable, so that its destructor will be executed when the program
102 PMProfStat(char const * name)
103 : name_(name), sec_(0), usec_(0), count_(0) {};
107 std::cerr << "##### " << name_ << ": "
108 << 1.0 * (sec_ * 1000000 + usec_)/ count_
109 << "usec, count=" << count_ << std::endl;
112 void add(const long long s, const long long u) {
120 long long sec_, usec_;
121 unsigned long long count_;
125 /* Helper class which gathers data at the end of the scope. One
126 * instance of this one should be created at each execution of the
127 * block. At the end of the block, it sends statistics to the static
130 class PMProfInstance {
132 PMProfInstance(PMProfStat * stat) : stat_(stat)
134 gettimeofday(&before_, 0);
138 gettimeofday(&after_, 0);
139 stat_->add(after_.tv_sec - before_.tv_sec,
140 after_.tv_usec - before_.tv_usec);
144 timeval before_, after_;
149 #define PROFILE_THIS_BLOCK(a) \
150 static PMProfStat PMPS_##a(#a);\
151 PMProfInstance PMPI_##a(&PMPS_##a);