for i=1,#table a lot slower than for k,v in pairs (table)?
Posted: Mon Dec 26, 2016 10:37 am
The behaviour of this function to merge red and green wires is confusing me.
This is the version I was using up until now.
According to any lua performance guide using for i, #table do should be several times faster, but when I use tried to optimzie the function like the following version it became nearly 10 times slower.
This is the version I was using up until now.
Code: Select all
function GetCircuitValues(entity)
local greenWire = entity.get_circuit_network(defines.wire_type.green)
local redWire = entity.get_circuit_network(defines.wire_type.red)
local items = {}
if greenWire then
for _, v in pairs (greenWire.signals) do
items[v.signal.type..","..v.signal.name] = v.count
end
end
if redWire then
for _, v in pairs (redWire.signals) do
if items[v.signal.type..","..v.signal.name] ~= nil then
items[v.signal.type..","..v.signal.name] = items[v.signal.type..","..v.signal.name] + v.count
else
items[v.signal.type..","..v.signal.name] = v.count
end
end
end
return items
end
Code: Select all
function GetCircuitValues(entity)
local greenWire = entity.get_circuit_network(defines.wire_type.green)
local redWire = entity.get_circuit_network(defines.wire_type.red)
local items = {}
if greenWire then
for i=1, #greenWire.signals do
local type = greenWire.signals[i].signal.type
local name = greenWire.signals[i].signal.name
local count = greenWire.signals[i].count
items[type..","..name] = count
end
end
if redWire then
for i=1, #redWire.signals do
local type = redWire.signals[i].signal.type
local name = redWire.signals[i].signal.name
local count = redWire.signals[i].count
if items[type..","..name] ~= nil then
items[type..","..name] = items[type..","..name] + count
else
items[type..","..name] = count
end
end
end
return items
end