3 This file is part of libmspcore
4 Copyright © 2007 Mikko Rasa, Mikkosoft Productions
5 Distributed under the LGPL
8 #ifndef MSP_DEBUG_PROFILER_H_
9 #define MSP_DEBUG_PROFILER_H_
14 #include "../time/timedelta.h"
22 A class for collecting timing data from a program. It's not as efficient as
23 external profilers, but allows profiling of custom scopes and retrieving the
24 profiling data at run time. An example usage could be showing realtime
25 performance statistics of a game or a simulation.
27 See also class ProfilingScope.
29 Note: This is not thread-safe. To profile multiple threads, create a separate
30 Profiler for each thread.
38 Time::TimeDelta total_time;
39 Time::TimeDelta self_time;
40 Time::TimeDelta avg_time;
41 std::vector<Time::TimeDelta> history;
43 std::map<std::string, unsigned> called_from;
50 std::map<std::string, ScopeInfo> scopes;
51 ProfilingScope *inner;
57 Sets the averaging period for timing data, measured in calls. Previous
58 average timings are cleared.
60 void set_period(unsigned p);
63 Adds a scope without recording any calls to it. Useful if you might need to
64 access a scope before it has finished for the first time.
66 void add_scope(const std::string &name);
69 Changes the recorded innermost scope pointer and returns the old one. This
70 is used by ProfilingScope to track child time and should not be called
73 ProfilingScope *enter(ProfilingScope *ps);
76 Records a call to a scope. You'll probably want to use a ProfilingScope
77 instead of calling this manually.
80 @param pn Parent scope name
81 @param t Time spent in the scope
82 @param ct Time spent in child scopes
84 void record(const std::string &sn, const std::string &pn, const Time::TimeDelta &t, const Time::TimeDelta &ct);
87 Returns informations about a scope.
89 const ScopeInfo &scope(const std::string &) const;