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?
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)
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.
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.
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.
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.
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.
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.
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.
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).