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
#include "global.h"
20
#include "debugtimer.h"
21
 
22
// static members
23
QHash<QString, double> DebugTimer::mTimingList;
24
 
25
/*
26
double DebugTimer::m_tick_p_s=0.;
27
 
28
void DebugTimer::sampleClock(int ms)
29
{
30
    QTime t;
31
    t.start();
32
    ticks now = getticks();
33
 
34
    while (t.elapsed() < ms) {
35
       int t;
36
       double x=0.;
37
       for (t=0; t<100; t++)
38
           x+=sin(x);
39
    }
40
    int el = t.elapsed();
41
    double tickselapsed = elapsed(getticks(), now);
42
    m_tick_p_s = tickselapsed / double(el);
43
    qDebug() << ms << "ms -> ticks/msec" << m_tick_p_s << "ticks elapsed" << tickselapsed;
44
 
45
}*/
46
DebugTimer::~DebugTimer()
47
{
48
    double t = elapsed();
49
    mTimingList[m_caption]+=t;
50
    // show message if timer is not set to silent, and if time > 100ms (if timer is set to hideShort (which is the default))
51
    if (!m_silent && (!m_hideShort || t>100.))
52
        showElapsed();
53
}
54
 
55
QMutex timer_mutex;
56
DebugTimer::DebugTimer(const QString &caption, bool silent)
57
{
58
    m_caption = caption;
59
    m_silent=silent;
60
    m_hideShort=true;
61
    if (!mTimingList.contains(caption)) {
62
        QMutexLocker locker(&timer_mutex);
63
        if (!mTimingList.contains(caption))
64
            mTimingList[caption]=0.;
65
    }
66
    start();
67
}
68
 
69
void DebugTimer::clearAllTimers()
70
{
71
    QHash<QString, double>::iterator i = mTimingList.begin();
72
     while (i != mTimingList.end()) {
73
         i.value() = 0.;
74
         ++i;
75
     }
76
}
77
void DebugTimer::printAllTimers()
78
{
79
    QHash<QString, double>::iterator i = mTimingList.begin();
80
    qWarning() << "Total timers\n================";
81
    double total=0.;
82
    while (i != mTimingList.end()) {
83
         if (i.value()>0)
824 werner 84
             qWarning() << i.key() << ":" << timeStr(i.value());
808 werner 85
         total+=i.value();
86
         ++i;
87
     }
88
    qWarning() << "Sum: " << total << "ms";
89
}
90
 
824 werner 91
// pretty formatting of timing information
92
QString DebugTimer::timeStr(double value_ms)
93
{
94
    if (value_ms<10000)
95
        return QString("%1ms").arg(value_ms);
96
    if (value_ms<60000)
97
        return QString("%1s").arg(value_ms/1000);
98
    if (value_ms<60000*60)
99
        return QString("%1m %2s").arg(floor(value_ms/60000)).arg(fmod(value_ms,60000)/1000);
100
 
101
    return QString("%1h %2m %3s").arg(floor(value_ms/3600000)) //h
102
            .arg(floor(fmod(value_ms,3600000)/60000)) //m
103
            .arg(qRound(fmod(value_ms,60000)/1000));    //s
104
}
105
 
808 werner 106
void DebugTimer::interval(const QString &text)
107
{
108
    double elapsed_time = elapsed();
824 werner 109
    qDebug() << "Timer" << text << timeStr(elapsed_time);
808 werner 110
    start();
111
}
112
 
113
void DebugTimer::showElapsed()
114
{
115
    if (!m_shown) {
824 werner 116
            qDebug() << "Timer" << m_caption << ":" << timeStr(elapsed());
808 werner 117
    }
118
    m_shown=true;
119
}
120
double DebugTimer::elapsed()
121
{
122
    return t.elapsed()*1000;
123
}
124
 
125
void DebugTimer::start()
126
{
127
    t.start();
128
    m_shown=false;
129
}