Profiling your Lua script

With Lua scripts that run slowly, it is useful to determine where the bottlenecks are.  To do this, we can use the Pepperfish profiler.  To add the profiler to your project, copy the code found here into a new shared module.  Then, you must require the module in the file containing the code to be profiled.  To profile a piece of code, simply add the following before the code,

profiler = newProfiler()
profiler:start()

and the following after it,

profiler:stop()

local outfile = io.open( "profile.txt", "w+" )
profiler:report( outfile )
outfile:close()

As you may have inferred, the results are stored in a file called profile.txt.  This is configurable.

The profiler can work in two modes: call and time mode.  The former keeps track of the number of times functions are called.  To use this mode, change the line above to

profiler = newProfiler('call')

Leave A Comment?