Does a longer script affect performance?

Just wondering if there is any performance impact to having longer overall scripts. For the sake of example, let’s say that you expand out a loop.

Script A:
for i = 1, 10 do
dosomething()
end

Script B:
dosomething()
dosomething()
dosomething()
dosomething()
dosomething()
dosomething()
dosomething()
dosomething()
dosomething()
dosomething()

Ignoring any performance benefits that may come from not using a loop altogether, is it safe to say that Script A and Script B have identical performance? I do realize that Script B would probably generate a longer bytecode and thus use more memory?

2 Likes

no, I don’t think so. You keep scripts shorter because it’s to save time and making script easier to read

1 Like

Performance is affected by how many scripts is running at once, and how expensive they are. For example, a while true do loop with a wait(0.01) inside will be really expensive, and if you have lots of it, it might affect performance.

To simply it doesn’t matter how long it is as long as they aren’t all running at one time. I have a code with more than 1100 lines with the use of functions to minimize, and it works fine.

Not satisfied with this answer. You make a sensible assertion that I am inclined to agree with, but I need someone with good knowledge of the inner workings to be sure.

wait(0.01) is not expensive at all, mainly because it’s the same as wait(1/30).

The cost of calling dosomething() is orders of magnitude more important than the cost of running it in a for loop or otherwise, it would be a micro-optimization at best. If the function instantiates 40,000 parts and parents them to workspace, it won’t matter how you run those functions, it’ll just be laggy.

You can test this yourself if you want, like running a print statement 1,000 times as written or in a for loop and measuring the amount of time between for comparison:

local start = os.clock()

for i = 1, 1_000 do
    print("foo")
end

print(os.clock() - start)
local start = os.clock()

print("foo")
print("foo")
print("foo")
-- ...
print("foo")

print(os.clock() - start)

I would assume the result is you could change the length of the string being printed, and that would affect time elapsed much, much more than whether it’s in a for loop or not.

2 Likes
while true do
  wait(0.01) -- minimum wait is 29 milliseconds, not 10
end

Nothing wrong with this code nor is it a performance killer.

This is a theoretical question, not a practical one. I want to know if the performance is identical or not, ignoring the cost of the for loop.

Trying to benchmark this isn’t very useful because of how miniscule the time difference would be. I want theory, not experimental results.

1 Like

The loop is less efficient because it needs to add a variable, compare a variable, and jump to a position ten times.
The longer script just does something ten times, no variables, no comparisons, and no jumping around.

DO NOT think this means you should favor the second way. The efficiency gain is not worth the extra effort and mess that doing it the long way will cause. If you’re trying to be perfectly optimized, this is not the place to start. You will never get there. And if this kind of femto-second performance gain is something you care about, Lua is not the language you should be using. Lua is a slow and clunky language.

1 Like

Goodness sakes, can anyone here read? I said to ignore the cost of the loop. I just want to know if the length of a script affects runtime execution speed.

Okay, here’s a different question:

Two scripts.
Script A:
dosomething()

Script B:
dosomething()
dosomething()
dosomething()
dosomething()

Is it true that Script B will take 4 times as long to complete as Script A, assuming no other randomness or caching that would affect the speed of an individual dosomething() call? Or does the increased length of Script B make the function call slower, thus making it take longer than 4x Script A to complete.

1 Like

Yes. They will cost the same. Compile time will be longer marginally.

Is it not a performance killer if the game starts lagging? Let’s say you spam wait(0.001) instead…in a while true do loop

In that sense it is true because the script runs one function at a time.

Why is this even a question?

any “good” programmer would use option A

1 Like

No need to insert yourself into a thread just to berate my question. Having a technical understanding of the Lua compiler is useful for being able to write optimal code. Was just looking to expand my knowledge.

2 Likes

I’m not sure what to say. Just don’t be afraid of while loops and wait. If it’s laggy it’s because of what you are doing inside of that loop. Which means you need to optimize your code. You might be able to find a better algorithm for what you are doing or redesign what you want to do.

1 Like

It depends what the script itself is doing, if its creating parts then it’ll lag but if you just require a long script for a task it should be fine.

1 Like

It heavily depends on the code itself so you cannot make a generalisation.
In your example, from a non optimising compiler perspective, Script B will run faster but will take up more memory, so you gain speed in exchange for memory usage. This is called Space time tradeoff. As to why, it’s because the code does not execute a jump instruction every iteration and does not count the number of iterations done, so it will run faster. This is called Loop unrolling.
Now, I’m gonna assume that Luau has a decently optimising compiler and that it will unroll your loop automatically, so there should be no difference in size and speed at all. You can read about more optimisation methods here Optimizing compiler - Wikipedia
It is safe to say that there is no correlation between code length and performance.

5 Likes

Thank you. This was the answer I was looking for. I’m writing some code that needs to be executed hundreds of times per frame (think physics, think rendering) and I’m trying to microoptimize as much as possible, just wanted to make sure I wasn’t shooting myself in the foot (I’m not expanding out loops btw, so please don’t have a panic attack xD, the loop example was just an example).

1 Like