Maximising Performance

I’ll keep this simple.

I’m trying to make a game that uses tens of thousands of parts, and lots of scripts. So I want performance to be maximised in this kind of game.

Yes, there are lots of parts.

print(#workspace:GetDescendants())
--17491

So, I have got two parameters that do the same thing, but I don’t know which one is better.

Take a look:

Option A:

local i = 0

repeat i += 1 print(i) task.wait() until i == 20

Option B:

for i = 1,20 do
    print(i)
    task.wait()
end

I know Option B looks simpler, but I don’t know if it takes up performance, or something like that.

If you know, then please go ahead and tell me. Thank you.

2 Likes

while loops are the best

local i: number = 0;

while (i<20) do
   print(i);
   i++;
   task.wait();
end

Is it because it shows the index?

while loops are the primitive loops from which for loops and repeat are made

The second option is better, because you won’t need to add a value each iteration, and you also do not need to create a starting variable. Keep in mind for both tests below, I removed the task.wait().

Time taken for 100000000 iterations using for i, n:
0.2458439999991242

Time taken for 100000000 iterations using while i < n:
0.425913799999762

I would worry about how your game is actually structured though, because these small differences will not affect your game as much as having bad organization or code structuring (like having an individual script for every part).

I would really recommend watching this though:

There is pretty much no difference and you should focus on optimizing the core parts of your game.

If you want the exact numbers, use os.clock() to benchmark the different options. For example,

local t = os.clock()

for i = 1, 20 do
    task.wait()
end

print(os.clock() - t)

You won’t see much of a difference if you run the options several times.

Use option 2 for better readability.

Even in C (the language I’m assuming you’re using), for loops are faster (arguably more readable too) than while loops. You shouldn’t just tell OP an answer without benchmarking/research.

While it is true that in C for loops are faster, in luau it actually doesn’t make any difference, I have just tested it

Could you show your results and code used to test? I tested it and my results show that while loops are around 0.5x the speed of for loops over 100000000 iterations (without the task.wait()). Our times may vary though, because our PCs will be different specs, but the ratio of performance should be similar.

Honestly I have tested it with 20 iterations without the task.wait(), let me try with 100000000

1 Like

You’re definitely right, my fault.
These are my results with the code I used:
image

1 Like

The difference will likely be fractions of a millisecond — almost certainly not enough to make any meaningful difference. Usually, you don’t want to make your code more complex just for minimal performance gains.

From the wording in your post, it doesn’t even sound like you’ve tried either solution to see how it works. So, go back and finish your script, and try the simpler for loop and see if it causes performance issues. Chances are, it will not. If it does, there are probably some larger system design questions to talk about that would be more fruitful than refactoring it into a repeat or while loop.

Let us know how it goes. Good luck!

1 Like

I agree with @kiloe2. Unless you notice your game is laggy, don’t worry about performance. I would recommend opening up the script performance tab though.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.