2017-06-25 26 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