mirror of
https://github.com/amyinspace/MagicSetEditor2.git
synced 2026-06-10 13:06:59 -04:00
Split script profiler into a separate file
git-svn-id: svn://svn.code.sf.net/p/magicseteditor/code/trunk@1201 0fc631ac-6414-0410-93d0-97cfa31319b6
This commit is contained in:
+1
-89
@@ -9,6 +9,7 @@
|
||||
#include <util/prec.hpp>
|
||||
#include <script/context.hpp>
|
||||
#include <script/to_value.hpp>
|
||||
#include <script/profiler.hpp>
|
||||
#include <util/error.hpp>
|
||||
#include <iostream>
|
||||
|
||||
@@ -18,95 +19,6 @@ Context::Context()
|
||||
: level(0)
|
||||
{}
|
||||
|
||||
// ----------------------------------------------------------------------------- : Profiler
|
||||
|
||||
#if USE_SCRIPT_PROFILING
|
||||
|
||||
#ifndef UNICODE
|
||||
#error "It looks like you are building the final release; disable USE_SCRIPT_PROFILING!"
|
||||
#endif
|
||||
|
||||
#ifdef WIN32
|
||||
typedef LONGLONG ProfileTime;
|
||||
|
||||
ProfileTime timer_now() {
|
||||
LARGE_INTEGER i;
|
||||
QueryPerformanceCounter(&i);
|
||||
return i.QuadPart;
|
||||
}
|
||||
ProfileTime timer_resolution() {
|
||||
LARGE_INTEGER i;
|
||||
QueryPerformanceFrequency(&i);
|
||||
return i.QuadPart;
|
||||
}
|
||||
#else
|
||||
#error "Can't use profiler"
|
||||
#endif
|
||||
|
||||
ProfileTime delta; ///< Time excluded
|
||||
|
||||
class Timer {
|
||||
public:
|
||||
Timer() {
|
||||
start = timer_now() + delta;
|
||||
}
|
||||
ProfileTime time() {
|
||||
ProfileTime end = timer_now() + delta;
|
||||
ProfileTime diff = end - start;
|
||||
start = end;
|
||||
return diff;
|
||||
}
|
||||
void exclude_time() {
|
||||
ProfileTime delta_delta = time();
|
||||
delta -= delta_delta; // this time is not counted, even recursively
|
||||
start -= delta_delta;
|
||||
}
|
||||
private:
|
||||
ProfileTime start;
|
||||
};
|
||||
|
||||
/// How much time was spent in each function?
|
||||
struct FunctionProfile {
|
||||
FunctionProfile() : time(0), calls(0) {}
|
||||
ProfileTime time;
|
||||
UInt calls;
|
||||
};
|
||||
VectorIntMap<unsigned int, FunctionProfile> variable_timings;
|
||||
|
||||
/// Profile a single function
|
||||
struct Profiler {
|
||||
public:
|
||||
inline Profiler(Timer& timer, Variable function)
|
||||
: timer(timer), function(function)
|
||||
{
|
||||
timer.exclude_time();
|
||||
}
|
||||
inline ~Profiler() {
|
||||
ProfileTime time = timer.time();
|
||||
if ((int)function < 0) return;
|
||||
// per function timing
|
||||
FunctionProfile& funprof = variable_timings[function];
|
||||
funprof.time += time;
|
||||
funprof.calls += 1;
|
||||
timer.exclude_time();
|
||||
}
|
||||
private:
|
||||
Timer& timer;
|
||||
Variable function;
|
||||
};
|
||||
|
||||
/// Get profile time in seconds and function names
|
||||
void get_profile(vector<FunctionProfileItem>& out) {
|
||||
double resolution = timer_resolution();
|
||||
const vector<FunctionProfile>& times = variable_timings.get();
|
||||
for (size_t i = 0 ; i < times.size() ; ++i) {
|
||||
if (times[i].calls == 0) continue;
|
||||
out.push_back(FunctionProfileItem(variable_to_string((Variable)i), times[i].time / resolution, times[i].calls));
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
// ----------------------------------------------------------------------------- : Evaluate
|
||||
|
||||
// Perform a unary simple instruction, store the result in a (not in *a)
|
||||
|
||||
Reference in New Issue
Block a user