Subversion Repositories public iLand

Rev

Rev 1221 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
1211 werner 1
/********************************************************************************************
2
**    iLand - an individual based forest landscape and disturbance model
3
**    http://iland.boku.ac.at
4
**    Copyright (C) 2009-  Werner Rammer, Rupert Seidl
5
**
6
**    This program is free software: you can redistribute it and/or modify
7
**    it under the terms of the GNU General Public License as published by
8
**    the Free Software Foundation, either version 3 of the License, or
9
**    (at your option) any later version.
10
**
11
**    This program is distributed in the hope that it will be useful,
12
**    but WITHOUT ANY WARRANTY; without even the implied warranty of
13
**    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
**    GNU General Public License for more details.
15
**
16
**    You should have received a copy of the GNU General Public License
17
**    along with this program.  If not, see <http://www.gnu.org/licenses/>.
18
********************************************************************************************/
808 werner 19
#ifndef DEBUGTIMER_H
20
#define DEBUGTIMER_H
21
#include "ticktack.h"
22
 
23
/** Timer class that writes timings to the Debug-Output-Channel
24
 
25
The class writes the elapsed time to qDebug() when either destructed, or when explicitely showElapsed() is called.
26
  elapsed() queries the elapsed time in milliseconds since construction or start() is called. Using interval() one can
27
  write a message with the time elapsed up the calling time, and the clock is reset afterwards. The name of the timer is
28
  set during construction. This message is printed when showElapsed() is called or durig destruction.
29
  Additionally, elapsed times of timers sharing the same caption are aggregated. Use clearAllTimers() to reset and printAllTimers()
30
  print the sums to the debug console. "Silent" DebugOutputs (setSilent() don't print timings for each iteration, but are still
31
    counted in the sums. If setAsWarning() is issued, the debug messages are print as warning, thus also visible
32
  when debug messages are disabled.
33
  @code void foo() {
34
     DebugTimer t("foo took [ms]:");
35
     <some lengthy operation>
36
 } // will e.g. print "foo took [ms]: 123" to debug console
37
 
38
 void bar() {
39
    DebugTimer::clearAllTimers(); // set all timers to 0
40
    for (i=0;i<1000;i++)
41
       foo();
42
    DebugTimer::printAllTimers(); // print the sum of the timings.
43
 }
44
 @endcode
45
 For Windows, the "TickTack"-backend is used.
46
 
47
*/
48
class DebugTimer
49
{
50
public:
51
    DebugTimer() { m_hideShort=false; m_silent=false; start(); }
52
    DebugTimer(const QString &caption, bool silent=false);
53
    void setSilent() { m_silent=true; }
54
    void setHideShort(bool hide_short_messages) { m_hideShort = hide_short_messages; }
55
    ~DebugTimer();
56
    void showElapsed();
57
    double elapsed(); // elapsed time in milliseconds
58
    void start();
59
    void interval(const QString &text);
60
    static void clearAllTimers();
61
    static void printAllTimers();
824 werner 62
    static QString timeStr(double value_ms);
808 werner 63
private:
64
    static QHash<QString, double> mTimingList;
65
    TickTack t;
66
    bool m_hideShort; // if true, hide messages for short operations (except an explicit call to showElapsed())
67
    bool m_shown;
68
    bool m_silent;
69
    QString m_caption;
70
};
71
 
72
#endif // DEBUGTIMER_H