Profiling
Not all Lua debugging is about errors. Sometimes, debugging is more optimization work. In order to tell what part of your code is running slow, or to detect any code hot spots, you have to profile your code. Profiling code means measuring how long something took to execute, or how many times something has executed. We can use Lua's debug library to build a simple profiler.
The profile module
We will implement the profiler as a new module. Create a new file, profiler.lua
, and declare the profiler
table. This table will contain four other tables: one table for the names of every function, one for the number of times a function is called, one for the total time spent on the function, and one for timing the functions.
The key to each of these tables is going to be a function object:
local profiler = {} profiler.names = { } profiler.counts = { } profiler.times = { } profiler.timers = { }
The profiler
module will have two important public functions: start
and stop
. These functions will start...