1-- GunFu Deadlands 2-- Copyright 2009-2011 Christiaan Janssen, September 2009-October 2011 3-- 4-- This file is part of GunFu Deadlands. 5-- 6-- GunFu Deadlands is free software: you can redistribute it and/or modify 7-- it under the terms of the GNU General Public License as published by 8-- the Free Software Foundation, either version 3 of the License, or 9-- (at your option) any later version. 10-- 11-- GunFu Deadlands is distributed in the hope that it will be useful, 12-- but WITHOUT ANY WARRANTY; without even the implied warranty of 13-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14-- GNU General Public License for more details. 15-- 16-- You should have received a copy of the GNU General Public License 17-- along with GunFu Deadlands. If not, see <http://www.gnu.org/licenses/>. 18 19ProfilingEnabled = true 20DebugInfo = {} 21DebugStrings = {} 22 23function doTrace( when ) 24 local funcname = debug.getinfo(2,"n").name 25 if not funcname then 26 return 27 end 28 29 if when == "call" then 30 beginBlock(funcname) 31 elseif when == "return" then 32 endBlock(funcname) 33 end 34end 35 36function beginBlock( funcname ) 37 if not DebugInfo[funcname] then 38 table.insert( DebugStrings, funcname ) 39 DebugInfo[funcname] = {calls=0, totaltime=0, timers={}} 40 end 41 table.insert(DebugInfo[funcname].timers, os.clock()) 42end 43 44function endBlock( funcname ) 45 local entry = DebugInfo[ funcname ] 46 if entry then 47 local ndx = table.getn(entry.timers) 48 if (ndx > 0) then 49 local duration = os.clock() - entry.timers[ndx] 50 table.remove( entry.timers, ndx ) 51 entry.totaltime = entry.totaltime + duration 52 entry.calls = entry.calls + 1 53 end 54 end 55end 56 57function dumpTrace() 58 print("funcname totaltime meantime callcount") 59 for i,funcname in ipairs(DebugStrings) do 60 if DebugInfo[funcname].calls > 0 then 61 print( funcname .. " " .. DebugInfo[funcname].totaltime .. " " .. DebugInfo[funcname].totaltime/DebugInfo[funcname].calls*1e6 .. " " .. DebugInfo[funcname].calls ) 62 end 63 end 64end 65 66debug.sethook( doTrace, "cr" )