module CP_Profiler
@@stacks = {}
@@maps = {}
@@start = 0

def self.begin
@@stacks = {}
@@maps = {}
@@start = Process.times
set_trace_func proc { |event, file, line, id, binding, klass|
case event
when "c-call", "call"
now = Process.times
stack = (@@stacks ||= )
stack.push
when "c-return", "return"
now = Process.times
key = "#{klass}##{id}"
stack = (@@stacks ||= )
if tick = stack.pop
threadmap = (@@maps ||= {})
data = (threadmap ||= )
data += 1
cost = now - tick
data += cost
data += cost - tick
stack += cost if stack
end
end
}
end

def self.print
set_trace_func nil
total = Process.times
total = 0.01 if total == 0
totals = {}
@@maps.values.each do |threadmap|
threadmap.each do |key,data|
total_data = (totals ||= )
total_data += data
total_data += data
total_data += data
end
end
data = totals.values
data = data.sort_by{ |x| -x }
sum = 0
puts sprintf " %% cumulative self self total\n"
puts sprintf " time seconds seconds calls ms/call ms/call name\n"
for d in data
break if d <= 0.0
sum += d
puts sprintf "%8.2f %8.2f %8.2f %8d %8.2f %8.2f %s\n",
d/total*100, sum, d, d, d*1000/d, d*1000/d, d
end
msgbox "Results printed to console.\nTracking reset."
end
end

class Scene_Base
alias_method :cp_profiler_342341_update, :update
def update(*args)
cp_profiler_342341_update(*args)
if Input.trigger?(:CTRL)
CP_Profiler.print
CP_Profiler.begin
end
end
end

CP_Profiler.begin