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 * * when the program ends, statistics will be sent to standard error, like:
24 * ##### some_identifier: 6.48475usec, count=25405
26 * The code measured by the profiler corresponds to the lifetime of a
27 * local variable declared by the PROFILE_THIS_BLOCK macro.
30 /* Helper class for gathering data. Instantiate this as a static
31 * variable, so that its destructor will be executed when the program
36 PMProfStat(char const * name)
37 : name_(name), sec_(0), usec_(0), count_(0) {};
41 std::cerr << "##### " << name_ << ": "
42 << 1.0 * (sec_ * 1000000 + usec_)/ count_
43 << "usec, count=" << count_ << std::endl;
46 void add(const long long s, const long long u) {
54 long long sec_, usec_;
55 unsigned long long count_;
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
64 class PMProfInstance {
66 PMProfInstance(PMProfStat * stat) : stat_(stat)
68 gettimeofday(&before_, 0);
72 gettimeofday(&after_, 0);
73 stat_->add(after_.tv_sec - before_.tv_sec,
74 after_.tv_usec - before_.tv_usec);
78 timeval before_, after_;
83 #define PROFILE_THIS_BLOCK(a) \
84 static PMProfStat PMPS_##a(#a);\
85 PMProfInstance PMPI_##a(&PMPS_##a)