Snippet: Profiler

Here is a simple profiler one can use to measure lengthy executions during development.

–[[----------------------------------------------------------------------------

Script : profiler.lua
Author : Alexander Stoica
Creation Date : 08/22/2009
Last modified : 09/14/2009
Version : 0.3

A very simple profiler to measure lengthy operations.

How to use:

Copy this file to your own tools folder and add the following line somewhere
near the start of your script.

local profiler = require(“profiler”)

To actually measure something, surround your code with the following calls.

profiler.run()
– lengthy code
profiler.stop()

The measured result will be shown in the terminal.

----------------------------------------------------------------------------]]–
local PROFILER = {}
----------------------------------------------------------------------------]]–

time_started = nil
time_stopped = nil

–[[profiling code]]------------------------------------------------------]]–

– runs the profiler, public
function PROFILER.run()
print(“Profiler running…”)
time_started = os.clock()
end

– stops the profiler and prints the time used, public
function PROFILER.stop()
time_stopped = os.clock()
print(“Profiler stopped…”)
print(“Time measured:”, time_stopped - time_started, “seconds.”)
end

----------------------------------------------------------------------------]]–
return PROFILER
---------------------------------------------------------------------[[EOF]]–