Page 1 of 1

Help with mod optimization

Posted: Sun Mar 26, 2017 9:01 am
by Bilka
My mod currently takes 0.030 ms of calculation time only to check if some global/ locals exist. Is there a way to make this process less performance intensive?

Code: Select all

script.on_event(defines.events.on_tick, function(event)
	if event.tick % 5 == 0 then
		local p1 = global.portal1.entity 
		local p2 = global.portal2.entity 
		if p1 and p2 then
			if p1.valid and p2.valid then
				for index, player in pairs(game.players) do
					if player.connected and player.character and player.vehicle == nil then
						try_teleport_1(player)
						try_teleport_2(player)
					end
				end
			end 
		end
	end
end)
Even if global.portal1.entity/global.portal2.entity don't exist the process takes about 0.029 ms. If they exist but aren't valid, the process takes about 0.032 ms, so it feels weird to me that cheking if they exist takes much longer than checking if they are valid. Doing this less often also doesn't increase performancce, the number stays around 0.028-0.030 ms.

Re: Help with mod optimization

Posted: Sun Mar 26, 2017 11:13 am
by Rseding91
0.030 ms is virtually nothing. Don't worry about it.

Re: Help with mod optimization

Posted: Sun Mar 26, 2017 11:22 am
by Klonan
Bilka wrote:My mod currently takes 0.030 ms of calculation time only to check if some global/ locals exist. Is there a way to make this process less performance intensive?

Even if global.portal1.entity/global.portal2.entity don't exist the process takes about 0.029 ms. If they exist but aren't valid, the process takes about 0.032 ms, so it feels weird to me that cheking if they exist takes much longer than checking if they are valid. Doing this less often also doesn't increase performancce, the number stays around 0.028-0.030 ms.
While it probably won't make a huge difference,
This might be a little faster:

Code: Select all

script.on_event(defines.events.on_tick, function(event)
  if event.tick % 5 ~= 0 then return end
  local p1 = global.portal1.entity 
  local p2 = global.portal2.entity 
  if (not p1) or (not p2) then return end
  if (not p1.valid) or (not p2.valid) then return end
  for index, player in pairs(game.connected_players) do
    if player.character and not player.vehicle then
    try_teleport_1(player)
    try_teleport_2(player)
    end
  end
end)

Re: Help with mod optimization

Posted: Sun Mar 26, 2017 6:26 pm
by Bilka
Thanks Klonan! While that code doesn't make a difference when global.portal1.entity/global.portal2.entity don't exist, it makes a difference when they do exist. It's also nice to know that I don't have to worry too much about the update time, otherwise I might have tinkered for ages.