Performance-wise questions / good practices
Posted: Sun Jan 29, 2017 2:41 am
Hello there, I have a question regarding script performance, more specifically i want to optimize as much as possible my on_tick event.
Basically i need delays/timers so I use an array like this : key= game.tick i want the code to perform, value= whatever data i need for the process
It's pretty handy to check on each tick if there's something to do
Most of the time, the value will be nil as nothing happens at this tick.
my question is, is this technique efficient? I don't know how Lua works internally, does it need to iterate the whole array to find out this key is (un)used ?
Considering the table might have hundreds of elements with gaps of thousands of possible keys between each, should I test a simple boolean first? something like :
I'm hesitating because reading from the global table seems to be quite expensive. In the end I don't know if using another variable is worth the trouble.
Does the size of the array matters?
Basically i need delays/timers so I use an array like this : key= game.tick i want the code to perform, value= whatever data i need for the process
It's pretty handy to check on each tick if there's something to do
Code: Select all
script.on_event(defines.events.on_tick, function(event)
if global.timer_array[event.tick] then
-- do stuff here using timer_array[event.tick]
end
end)
my question is, is this technique efficient? I don't know how Lua works internally, does it need to iterate the whole array to find out this key is (un)used ?
Considering the table might have hundreds of elements with gaps of thousands of possible keys between each, should I test a simple boolean first? something like :
Code: Select all
script.on_event(defines.events.on_tick, function(event)
if global.timer_state then
if global.timer_array[event.tick] then
-- do stuff here using timer_array[event.tick]
end
end
end)
I'm hesitating because reading from the global table seems to be quite expensive. In the end I don't know if using another variable is worth the trouble.
Does the size of the array matters?