2017-06-25 27 views
2

对于一个项目,我必须执行Lua脚本,并且需要一些关于执行的信息:内存大小,CPU时间和运行时。我没有在lua.org上找到有关Lua编译器的一些参数。有没有人知道这个功能的参数或编译器?后来,我想用PHP分析我的Lua脚本。通过PHP 在shell上执行Lua并获取内存大小和执行时间

    • 壳牌高管和获得信息反馈(MEM大小,CPU时间,运行时)

    谢谢!

  • 回答

    3

    要分析你的Lua脚本,只是把它包在下面的代码行:

    local time_start = os.time() 
    
    -------------------------------------- 
    -- Your script is here 
    -- For example, just spend 5 seconds in CPU busy loop 
    repeat until os.clock() > 5 
    -- or call some external file containing your original script: 
    -- dofile("path/to/your_original_script.lua") 
    -------------------------------------- 
    
    local mem_KBytes = collectgarbage("count") -- memory currently occupied by Lua 
    local CPU_seconds = os.clock()     -- CPU time consumed 
    local runtime_seconds = os.time() - time_start -- "wall clock" time elapsed 
    print(mem_KBytes, CPU_seconds, runtime_seconds) 
    -- Output to stdout: 24.0205078125 5.000009 5 
    

    现在可以执行这个脚本的shell命令lua path/to/this_script.lua和分析打印到stdout来获取信息的最后一行。

    +0

    感谢您的回复。 :) –

    +1

    更多时间分辨率的一些扩展:如果您安装了luasocket,则可以使用'socket.gettime'而不是'os.time'来获取毫秒数而不是第二个分辨率。使用你的shell的'time',就像'time lua path/to/script.lua'一样,也可以给出不同的有用时间(这些将包括启动时间)。 – nobody