Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How to optimize?

A while ago I made a little gun system, adding a few features to it. Eventually it became so complicated that it actually started to lag people. I decided to restart from scratch. I can do extremely basic optimizing, but it obviously wasn't enough. Now the performance is worse than my older gun system. Simply looking up and down raises the script's activity to a sudden 15-20% I used the same method in my older gun system (other than having the function not be inside the Equipped function) and it was much less activity. How would I go optimizing code better? Here's some basics I know:

--Unoptimized LocalScript Code:

game.Players.LocalPlayer.Character.Torso["Left Hip"].C1 = CFrame.new(0, 0.1, 0)
wait()
game.Players.LocalPlayer.Character.Torso["Left Hip"].C1 = CFrame.new(0, 0.2, 0)
wait()
game.Players.LocalPlayer.Character.Torso["Left Hip"].C1 = CFrame.new(0, 0.3, 0)
wait()
game.Players.LocalPlayer.Character.Torso["Left Hip"].C1 = CFrame.new(0, 0.4, 0)
wait()
game.Players.LocalPlayer.Character.Torso["Left Hip"].C1 = CFrame.new(0, 0.5, 0)
wait()

--Optimized LocalScript Code:

local LHip = game.Players.LocalPlayer.Character.Torso["Left Hip"]
local CF = CFrame.new

for i = 0.1, 0.5, 0.1 do
	LHip.C1 = CF(0, i, 0)
	wait()
end

I found this little code snippet on ROBLOX's blog about optimizing. However, the code in the picture doesn't work. The script simply errors and stops working. How can I optimize my code (better)?

1 answer

✓ Accepted answer

Optimization is a very difficult skill to learn, and really not in the scope of a single answer.

A basic methodology you can attempt is to never ever ever restart from scratch. That is the worst way to optimize code, as you'll make the same pitfalls as before, or worse as you seem to have run in to here.

Optimizing for speed, which is what you seem to be doing, is all about benchmarks.

Figure out multiple methods by which to calculate something in your code, and then run those calculations 1,000, 10,000, 100,000 or even 1,000,000 times each. Using tick(), measure how long each calculation batch took, and (using a randomized set of inputs with known 'correct' outputs) the 'accuracy' of the calculation.


To explain with a concrete example:

local f1 = function(a)
	return a*a*a*a*a
end

local f2 = function(a)
	return a^5	
end

--Both of these functions do exactly the same thing, but which is faster?

local t = tick()
for i = 1, 10000 do
	f1(i)
end
print("function 1 took: ", tick() - t)

local t = tick()
for i = 1, 10000 do
	f2(i)
end
print("function 2 took: ", tick() - t)

Obviously, in your case, the calculations will be a lot more complex. It's up to you to figure out how they might be made more efficient, either by eliminating extraneous function calls, or simply reducing the amount called per second, without sacrificing accuracy.


A somewhat unrelated topic you should look into, which will help with optimization as well to a lesser extent, is refactoring. This is the process by which you take a badly-made or otherwise unwieldy codebase and turn it into something better.

1 comment

0

I see... That's really helpful. Thanks adark!

This discussion is preserved as it was. New answers, comments, and votes are closed.