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.
21 /** How to use this trivial profiler:
23 * * at the beginning of the interesting block, just add:
24 * PROFILE_THIS_BLOCK(some_identifier)
26 * A trailing semicolon can be added at your discretion.
28 * * when the program ends, statistics will be sent to standard error, like:
30 * ##### some_identifier: 6.48475usec, count=25405
32 * The code measured by the profiler corresponds to the lifetime of a
33 * local variable declared by the PROFILE_THIS_BLOCK macro.
35 * Some examples of profiling scope: In the snippets below, c1, c2...
36 * designate code chunks, and the identifiers of profiling blocks are
37 * chosen to reflect what they count.
41 * PROFILE_THIS_BLOCK(c2)
47 * PROFILE_THIS_BLOCK(c1_c2)
49 * PROFILE_THIS_BLOCK(c2)
56 * PROFILE_THIS_BLOCK(c1)
59 * PROFILE_THIS_BLOCK(c2)
65 * PROFILE_THIS_BLOCK(c1_c2_c3)
68 * PROFILE_THIS_BLOCK(c2)
74 * Influence of identifier names: they are mainly used for display
75 * purpose, but the same name should not be used twice in the same
79 * PROFILE_THIS_BLOCK(foo)
81 * PROFILE_THIS_BLOCK(foo) // error: identifier clash
85 * In the example below, c1+c2 and c2 are counted separately, but in
86 * the output, both are confusingly labelled `foo'.
89 * PROFILE_THIS_BLOCK(foo)
92 * PROFILE_THIS_BLOCK(foo) // error: identifier clash
101 /* This function does not really returns the "time of day",
102 * but it will suffice to evaluate elapsed times.
104 int gettimeofday(struct timeval * tv, struct timezone * /*tz*/)
106 LARGE_INTEGER frequency, t;
107 QueryPerformanceFrequency(&frequency);
108 QueryPerformanceCounter(&t);
110 tv->tv_sec = long(t.QuadPart / frequency.QuadPart);
111 tv->tv_usec = long((1000000.0 * (t.QuadPart % frequency.QuadPart)) / frequency.QuadPart);
117 /* Helper class for gathering data. Instantiate this as a static
118 * variable, so that its destructor will be executed when the program
124 PMProfStat(char const * name)
125 : name_(name), sec_(0), usec_(0), count_(0) {};
129 std::cerr << "##### " << name_ << ": "
130 << 1.0 * (sec_ * 1000000 + usec_)/ count_
131 << "usec, count=" << count_ << std::endl;
134 void add(const long long s, const long long u) {
142 long long sec_, usec_;
143 unsigned long long count_;
147 /* Helper class which gathers data at the end of the scope. One
148 * instance of this one should be created at each execution of the
149 * block. At the end of the block, it sends statistics to the static
152 class PMProfInstance {
154 PMProfInstance(PMProfStat * stat) : stat_(stat)
156 gettimeofday(&before_, 0);
160 gettimeofday(&after_, 0);
161 stat_->add(after_.tv_sec - before_.tv_sec,
162 after_.tv_usec - before_.tv_usec);
166 timeval before_, after_;
171 #define PROFILE_THIS_BLOCK(a) \
172 static PMProfStat PMPS_##a(#a);\
173 PMProfInstance PMPI_##a(&PMPS_##a);