Mercurial > hg > svcore
comparison base/Profiler.cpp @ 0:da6937383da8
initial import
author | Chris Cannam |
---|---|
date | Tue, 10 Jan 2006 16:33:16 +0000 |
parents | |
children | d86891498eef |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:da6937383da8 |
---|---|
1 /* -*- c-basic-offset: 4 -*- vi:set ts=8 sts=4 sw=4: */ | |
2 | |
3 /* | |
4 A waveform viewer and audio annotation editor. | |
5 Chris Cannam, Queen Mary University of London, 2005 | |
6 | |
7 This is experimental software. Not for distribution. | |
8 */ | |
9 | |
10 /* | |
11 This is a modified version of a source file from the | |
12 Rosegarden MIDI and audio sequencer and notation editor. | |
13 This file copyright 2000-2005 Chris Cannam and Guillaume Laurent. | |
14 */ | |
15 | |
16 #include <iostream> | |
17 #include "Profiler.h" | |
18 | |
19 #include <vector> | |
20 #include <algorithm> | |
21 | |
22 //#define NO_TIMING 1 | |
23 | |
24 #ifdef NDEBUG | |
25 #define NO_TIMING 1 | |
26 #endif | |
27 | |
28 using std::cerr; | |
29 using std::endl; | |
30 | |
31 Profiles* Profiles::m_instance = 0; | |
32 | |
33 Profiles* Profiles::getInstance() | |
34 { | |
35 if (!m_instance) m_instance = new Profiles(); | |
36 | |
37 return m_instance; | |
38 } | |
39 | |
40 Profiles::Profiles() | |
41 { | |
42 } | |
43 | |
44 Profiles::~Profiles() | |
45 { | |
46 dump(); | |
47 } | |
48 | |
49 void Profiles::accumulate(const char* id, clock_t time, RealTime rt) | |
50 { | |
51 #ifndef NO_TIMING | |
52 ProfilePair &pair(m_profiles[id]); | |
53 ++pair.first; | |
54 pair.second.first += time; | |
55 pair.second.second = pair.second.second + rt; | |
56 | |
57 TimePair &timePair(m_lastCalls[id]); | |
58 timePair.first = time; | |
59 timePair.second = rt; | |
60 #endif | |
61 } | |
62 | |
63 void Profiles::dump() | |
64 { | |
65 #ifndef NO_TIMING | |
66 cerr << "Profiles::dump() :\n"; | |
67 | |
68 // I'm finding these two confusing dumped out in random order, | |
69 // so I'm going to sort them alphabetically: | |
70 | |
71 std::vector<const char *> profileNames; | |
72 for (ProfileMap::iterator i = m_profiles.begin(); | |
73 i != m_profiles.end(); ++i) { | |
74 profileNames.push_back((*i).first); | |
75 } | |
76 | |
77 std::sort(profileNames.begin(), profileNames.end()); | |
78 | |
79 for (std::vector<const char *>::iterator i = profileNames.begin(); | |
80 i != profileNames.end(); ++i) { | |
81 | |
82 cerr << "-> " << *i << ": CPU: " | |
83 << m_profiles[*i].first << " calls, " | |
84 << int((m_profiles[*i].second.first * 1000.0) / CLOCKS_PER_SEC) << "ms, " | |
85 << (((double)m_profiles[*i].second.first * 1000000.0 / | |
86 (double)m_profiles[*i].first) / CLOCKS_PER_SEC) << "us/call" | |
87 << endl; | |
88 | |
89 cerr << "-> " << *i << ": real: " | |
90 << m_profiles[*i].first << " calls, " | |
91 << m_profiles[*i].second.second << ", " | |
92 << (m_profiles[*i].second.second / m_profiles[*i].first) | |
93 << "/call" | |
94 << endl; | |
95 | |
96 cerr << "-> " << *i << ": last: CPU: " | |
97 << int((m_lastCalls[*i].first * 1000.0) / CLOCKS_PER_SEC) << "ms, " | |
98 << " real: " | |
99 << m_lastCalls[*i].second << endl; | |
100 } | |
101 | |
102 cerr << "Profiles::dump() finished\n"; | |
103 #endif | |
104 } | |
105 | |
106 Profiler::Profiler(const char* c, bool showOnDestruct) | |
107 : m_c(c), | |
108 m_showOnDestruct(showOnDestruct) | |
109 { | |
110 #ifndef NO_TIMING | |
111 m_startCPU = clock(); | |
112 | |
113 struct timeval tv; | |
114 (void)gettimeofday(&tv, 0); | |
115 m_startTime = RealTime(tv.tv_sec, tv.tv_usec * 1000); | |
116 #endif | |
117 } | |
118 | |
119 void | |
120 Profiler::update() | |
121 { | |
122 #ifndef NO_TIMING | |
123 clock_t elapsedCPU = clock() - m_startCPU; | |
124 | |
125 struct timeval tv; | |
126 (void)gettimeofday(&tv, 0); | |
127 RealTime elapsedTime = RealTime(tv.tv_sec, tv.tv_usec * 1000) - m_startTime; | |
128 | |
129 cerr << "Profiler : id = " << m_c | |
130 << " - elapsed so far = " << ((elapsedCPU * 1000) / CLOCKS_PER_SEC) | |
131 << "ms CPU, " << elapsedTime << " real" << endl; | |
132 #endif | |
133 } | |
134 | |
135 Profiler::~Profiler() | |
136 { | |
137 #ifndef NO_TIMING | |
138 clock_t elapsedCPU = clock() - m_startCPU; | |
139 | |
140 struct timeval tv; | |
141 (void)gettimeofday(&tv, 0); | |
142 RealTime elapsedTime = RealTime(tv.tv_sec, tv.tv_usec * 1000) - m_startTime; | |
143 | |
144 Profiles::getInstance()->accumulate(m_c, elapsedCPU, elapsedTime); | |
145 | |
146 if (m_showOnDestruct) | |
147 cerr << "Profiler : id = " << m_c | |
148 << " - elapsed = " << ((elapsedCPU * 1000) / CLOCKS_PER_SEC) | |
149 << "ms CPU, " << elapsedTime << " real" << endl; | |
150 #endif | |
151 } | |
152 |