【问题标题】:Execute Lua on shell and get memory size and execution time在shell上执行Lua并获取内存大小和执行时间
【发布时间】:2017-11-28 14:30:07
【问题描述】:

对于一个项目,我必须执行 Lua 脚本,并且我想要一些关于执行的信息:内存大小、CPU 时间和运行时。我在 lua.org 上没有找到有关 Lua 编译器的某些参数的任何信息。有谁知道具有此功能的参数或编译器?稍后,我想用 PHP 分析我的 Lua 脚本。

  • 通过 PHP 执行 Shell
  • 并获取信息(内存大小、cpu 时间、运行时间)

谢谢!

【问题讨论】:

    标签: php shell lua


    【解决方案1】:

    要分析您的 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 的最后一行以获取信息。

    【讨论】:

    • 感谢您的回复。 :)
    • 一些用于更多时间分辨率的扩展:如果你安装了 luasocket,你可以使用socket.gettime 而不是os.time 来获得毫秒而不是秒分辨率的时间。使用你的 shell 的time,如time lua path/to/script.lua 也可以给出不同的有用时间(这些将包括启动时间)。
    猜你喜欢
    • 1970-01-01
    • 2010-09-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-27
    • 2019-08-03
    • 2011-07-30
    • 2012-04-29
    相关资源
    最近更新 更多