lua 代码调优profile 工具

cocos2d-x lua工程没自带的profile 工具,前段时间项目有好多代码的用时都要测试,所以就自己写了一个,在此贴出,有用的直接拿去,省的重复造车轮

[lua]
module("gProfiles", package.seeall)

local timerTab = {}

function ProfilingBeginTimingBlock(timerName)
if timerTab[timerName] then
timerTab[timerName].lastTime = os.clock()
else
timerTab[timerName] = {
count = 0,
minTime = 99999999999,
maxTime = 0,
totalTime = 0,
lastTime = os.clock(),
name = timerName
}
end

end
function ProfilingEndTimingBlock(timerName)
local data = timerTab[timerName]
if data then

local deltaTime = os.clock() – data.lastTime
data.totalTime = data.totalTime + deltaTime
data.count = data.count + 1
if data.minTime >= deltaTime then
data.minTime = deltaTime
end
if data.maxTime <= deltaTime then
data.maxTime = deltaTime
end
else
error("timerName error!!!")
end

end
function displayTimers()
print("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ profiles start ")
for k,v in pairs(timerTab) do
print(tostring(v.name));
print(string.format("%s—%f","count",v.count));
print(string.format("%s—%f","minTime",v.minTime)) ;
print(string.format("%s—%f","maxTime",v.maxTime));
print(string.format("%s—%f","totalTime",v.totalTime));
print(string.format("%s—%f","averageTime",v.totalTime/v.count)) ;
print("———————————————")
end
print("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ profiles end ")
end

–dome code
–[[

gProfiles.ProfilingBeginTimingBlock("initRoleList")
self:initRoleList()
gProfiles.ProfilingEndTimingBlock("initRoleList")

gProfiles.displayTimers()
–]]

[/lua]
require() 一下就可以使用了

2 Replies to “lua 代码调优profile 工具”

Leave a Reply

Your email address will not be published. Required fields are marked *