File: SimpleProfiler.h

package info (click to toggle)
spring 106.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 55,260 kB
  • sloc: cpp: 543,946; ansic: 44,800; python: 12,575; java: 12,201; awk: 5,889; sh: 1,796; asm: 1,546; xml: 655; perl: 405; php: 211; objc: 194; makefile: 76; sed: 2
file content (114 lines) | stat: -rw-r--r-- 2,698 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
/* This file is part of the Spring engine (GPL v2 or later), see LICENSE.html */

#ifndef SIMPLE_PROFILER_H
#define SIMPLE_PROFILER_H

#ifdef	__cplusplus
extern "C" {
#endif

#if       !defined PROFILING_ENABLED && defined DEBUG
	// Always enable profiling in debug binaries
	#define PROFILING_ENABLED 1
#endif // !defined PROFILING_ENABLED && defined DEBUG

/**
 * Adds a specified ammount of time (in milli-seconds) to a certain part
 */
void          simpleProfiler_addTime(const char* const part, unsigned time);

/**
 * Returns the total ammount of time (in milli-seconds)
 * spent in a certain part so far
 */
unsigned long simpleProfiler_getTime(const char* const part);

/**
 * Returns the total number of profiling parts
 */
unsigned      simpleProfiler_getParts();

/**
 * Returns the name of a certain part by index.
 * @see simpleProfiler_getParts()
 */
const char*   simpleProfiler_getPartName(unsigned partIndex);

/**
 * Allows fetching the list of names of the profiling parts.
 * @return number of parts
 */
unsigned      simpleProfiler_getPartNames(const char** parts, const unsigned parts_sizeMax);

/**
 * Returns a summary string, consisting or name and total-time
 * of all profiling parts, each on a separate line.
 * Note: You have to free(...) the return value.
 */
char*         simpleProfiler_getSummaryString();

#ifdef __cplusplus
} // extern "C"
#endif


#if       defined __cplusplus

#if       defined PROFILING_ENABLED
	#define SCOPED_TIMER(part, profiler) ScopedTimer myScopedTimerFromMakro(part, profiler);
#else  // defined PROFILING_ENABLED
	#define SCOPED_TIMER(part, profiler)
#endif // defined PROFILING_ENABLED

#define DEFAULT_SCOPED_TIMER(part) SCOPED_TIMER(part, Profiler::GetDefault());


#include <map>
#include <string>

class Profiler
{
public:
	static Profiler* GetDefault();

	Profiler(const char* const name);

	const char* GetName() const;

	void AddTime(const char* const part, unsigned long time);
	const std::map<const char* const, unsigned long>& GetParts();

	std::string ToString() const;

private:
	const char* const name;
	std::map<const char* const, unsigned long> parts;

	static Profiler def;
};

/**
 * @brief Time profiling helper class
 *
 * Construct an instance of this class where you want to begin time measuring,
 * and destruct it at the end (or let it be autodestructed).
 */
class ScopedTimer
{
public:
	ScopedTimer(const char* const part, Profiler* profiler = Profiler::GetDefault());
	/**
	 * @brief destroy and add time to profiler
	 */
	~ScopedTimer();

private:
	const char* const part;
	Profiler* profiler;
	/// in milli-seconds
	unsigned long startTime;
};

#endif // defined __cplusplus

#endif // SIMPLE_PROFILER_H