Speed Concerns

bane_

Howardianism High-Priest
Joined
Nov 27, 2013
Messages
1,559
This is a very generic question about a specific characteristic (speed).
Which one is quicker/lighter for a system to run:

Code:
function a(i, s, b)
	if i == 1 then
		--do something
	end
	if s == 'line' then
		--do something
	end
	if b then
		--do something
	end
end

GameEvents.NiceDLLHook.Add(a)

or

Code:
function a(i, s, b)
	if i == 1 then
		--do something
	end
end

function a2(i, s, b)
	if s == 'line' then
		--do something
	end
end

function a3(i, s, b)
	if b then
		--do something
	end
end

GameEvents.NiceDLLHook.Add(a1)
GameEvents.NiceDLLHook.Add(a2)
GameEvents.NiceDLLHook.Add(a3)

I imagine the first one would always be quicker if the conditions are mutually exclusive (elseif or return statements), but, as is, which one is better, when specifically speaking about speed?
 
From my DLL tutorial on adding GameEvents

"If you consider C++ to be the jet-engined Gulfstream, then Lua is the turbo-prop Cessna. Both will get you there, but one will do it considerably quicker than the other. Just like changing aircraft, switching from C++ to Lua and vice-versa takes time, it doesn't really matter which plane you're on, you want to stay on it rather than change."

Unless you know that you can add/remove handlers a1, a2 and a3 depending on the state of the game (ie unless someone is in the Classical era you don't need a1, and as soon as all players enter the Industrial you can remove it again), the a handler will be quicker.
 
That is more of a microoptimization and you shouldnt really worry that much about it, you should program with "semantics" in mind, as in, your code has to be easy to understand 30 years from now... anyway I guess the first is faster as you only make one call...
 
Back
Top Bottom