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.
11 /** How to use this trivial profiler:
13 * * at the beginning of the interesting block, just add:
14 * PROFILE_THIS_BLOCK(some_identifier)
16 * A trailing semicolon can be added at your discretion.
18 * * when the program ends, statistics will be sent to standard error, like:
20 * ##### some_identifier: 6.48475usec, count=25405
22 * The code measured by the profiler corresponds to the lifetime of a
23 * local variable declared by the PROFILE_THIS_BLOCK macro.
25 * Some examples of profiling scope: In the snippets below, c1, c2...
26 * designate code chunks, and the identifiers of profiling blocks are
27 * chosen to reflect what they count.
31 * PROFILE_THIS_BLOCK(c2)
37 * PROFILE_THIS_BLOCK(c1_c2)
39 * PROFILE_THIS_BLOCK(c2)
46 * PROFILE_THIS_BLOCK(c1)
49 * PROFILE_THIS_BLOCK(c2)
55 * PROFILE_THIS_BLOCK(c1_c2_c3)
58 * PROFILE_THIS_BLOCK(c2)
64 * Influence of identifier names: they are mainly used for display
65 * purpose, but the same name should not be used twice in the same
69 * PROFILE_THIS_BLOCK(foo)
71 * PROFILE_THIS_BLOCK(foo) // error: identifier clash
75 * In the example below, c1+c2 and c2 are counted separately, but in
76 * the output, both are confusingly labelled `foo'.
79 * PROFILE_THIS_BLOCK(foo)
82 * PROFILE_THIS_BLOCK(foo) // error: identifier clash
102 #if defined(__GNUG__) && defined(_GLIBCXX_DEBUG)
103 #error Profiling is not usable when run-time debugging is in effect
107 /* This function does not really returns the "time of day",
108 * but it will suffice to evaluate elapsed times.
110 int gettimeofday(struct timeval * tv, struct timezone * /*tz*/)
112 LARGE_INTEGER frequency, t;
113 QueryPerformanceFrequency(&frequency);
114 QueryPerformanceCounter(&t);
116 tv->tv_sec = long(t.QuadPart / frequency.QuadPart);
117 tv->tv_usec = long((1000000.0 * (t.QuadPart % frequency.QuadPart)) / frequency.QuadPart);
123 /* Helper class for gathering data. Instantiate this as a static
124 * variable, so that its destructor will be executed when the program
130 PMProfStat(char const * name)
131 : name_(name), sec_(0), usec_(0), count_(0) {}
135 double total = 0.001 * (sec_ * 1000000 + usec_);
136 std::cerr << std::fixed << std::setprecision(2)
137 << "#pmprof# " << name_ << ": "
139 << "msec, count=" << count_
140 << ", total=" << total << "msec"
145 void add(const long long s, const long long u) {
153 long long sec_, usec_;
154 unsigned long long count_;
158 /* Helper class which gathers data at the end of the scope. One
159 * instance of this one should be created at each execution of the
160 * block. At the end of the block, it sends statistics to the static
163 class PMProfInstance {
165 PMProfInstance(PMProfStat * stat) : stat_(stat)
167 gettimeofday(&before_, 0);
171 gettimeofday(&after_, 0);
172 stat_->add(after_.tv_sec - before_.tv_sec,
173 after_.tv_usec - before_.tv_usec);
177 timeval before_, after_;
182 #define PROFILE_THIS_BLOCK(a) \
183 static PMProfStat PMPS_##a(#a);\
184 PMProfInstance PMPI_##a(&PMPS_##a);