简单Lua分析

简单Lua分析,lua,profiling,profiler,Lua,Profiling,Profiler,作为学校作业的一部分,我刚开始学习Lua。我想知道是否有一种简单的方法来实现Lua的评测?我需要的东西,显示分配的内存,在使用的变量,无论其类型,等等 我找到了C++解决方案,我已经成功编译了,但是我不知道如何将它们导入到Lua环境中。p> 我也找到了Shinny,但我找不到任何关于如何使其工作的文档。有几个可以检查,但大多数都以执行时间为目标(并且基于调试挂钩) 要跟踪变量,需要使用debug.getlocal和debug.getupvalue(从代码或调试挂钩) 要跟踪内存使用情况,可以使用

作为学校作业的一部分,我刚开始学习Lua。我想知道是否有一种简单的方法来实现Lua的评测?我需要的东西,显示分配的内存,在使用的变量,无论其类型,等等

我找到了C++解决方案,我已经成功编译了,但是我不知道如何将它们导入到Lua环境中。p> 我也找到了Shinny,但我找不到任何关于如何使其工作的文档。

有几个可以检查,但大多数都以执行时间为目标(并且基于调试挂钩)

要跟踪变量,需要使用
debug.getlocal
debug.getupvalue
(从代码或调试挂钩)

要跟踪内存使用情况,可以使用
collectgarbage(count)
(可能在
collectgarbage(collect)
之后),但这只会告诉您使用的内存总量。要跟踪单个数据结构,可能需要遍历全局和局部变量,并计算它们占用的空间量。您可以检查一些指针和实现细节

类似的东西是跟踪函数调用/返回的最简单的分析器(请注意,您不应该信任它生成的绝对数,而应该只信任相对数):


你查过了吗?哇!谢谢是否有一种方法可以编辑此代码以计算自设置挂钩以来已声明的变量数?您可能需要检查
debug.getlocal
debug.getupvalue
函数。因此,这个答案可能有用:。如果您想知道从某个时刻起设置了什么,您可能需要拍摄两张快照并进行比较。ProFi对我来说是一个很棒的探查器:
local calls, total, this = {}, {}, {}
debug.sethook(function(event)
  local i = debug.getinfo(2, "Sln")
  if i.what ~= 'Lua' then return end
  local func = i.name or (i.source..':'..i.linedefined)
  if event == 'call' then
    this[func] = os.clock()
  else
    local time = os.clock() - this[func]
    total[func] = (total[func] or 0) + time
    calls[func] = (calls[func] or 0) + 1
  end
end, "cr")

-- the code to debug starts here
local function DoSomethingMore(x)
  x = x / 2
end

local function DoSomething(x)
  x = x + 1
  if x % 2 then DoSomethingMore(x) end
end

for outer=1,100 do
  for inner=1,1000 do
    DoSomething(inner)
  end
end

-- the code to debug ends here; reset the hook
debug.sethook()

-- print the results
for f,time in pairs(total) do
  print(("Function %s took %.3f seconds after %d calls"):format(f, time, calls[f]))
end