In the long run, does it really matter if you optimize a little math.sin
or the stuff that actually needs to be optimized.
lol I was about to say that
In the long run, does it really matter if you optimize a little math.sin
or the stuff that actually needs to be optimized.
lol I was about to say that
But those lua functions are stored in hashtables and retrieved when you use them
Basically they are stored in the environment
_ENV["math"]["min"]
Why would you even do that? wth? It’s not that big of performance impact dude. Code isn’t that slow.
Optimize your functions, use modules,disconnect RBXScriptSignals and you should be fine;
It just makes it more painful
Yes doing all those things is great, but localizing global functions is always a good option
I literally never heard anyone say that’s good to do, you’re essentially just shorting they’re name. From what I know the only thing bad about globals is being localized, later it’s fast and ok.
Don’t you think Roblox would optimize Luau to do something like that already? And they did!
What you are doing is basically creating another variable which would use more memory;
And if you declare a variable, the parser has to manually go back to the point where the variable is declared, retrieve it’s value from the main table anyway, and then push it towards where the variable was called.
Alright so I get it now, but I wasnt totally wrong; doing this before the roblox Luau optimizations helped but now it has little to no impact
Thanks for clearing it up
Never has, never will be. Let’s break it down step-by-step:
In the memory, there is already a slot for print
that has the value to call the print
action.
When you do local print = print
, you are just creating yet another memory slot for the print
action. This wastes memory.
Was bored so I tested your benchmark code in Roblox Studio. Here are the results:
0.033565000165254
0.035869099898264
Code:
wait(10)
local t = os.clock()
-- Faster
for i = 1, 1000000 do
local x = math.sin(i)
end
print(os.clock()-t)
local tt = os.clock()
-- Slower
local sin = math.sin
for i = 1, 1000000 do
local x = sin(i)
end
print(os.clock()-tt)
Alright so it is faster localizing the functions, so should I do it?
I know its such a small amount
Asked 5 years, 3 months ago
Also the question is about Lua, Roblox Lua and normal Lua are very different, and Lua 5 years ago vs now is very different as well.
Roblox Lua has same functions but are actively modified, maintained and optimized, and with the introduction of Luau, it is safe to say Roblox no longer even uses Lua anymore, it uses a heavily modified version “Luau”.
Mate, the difference between those times is literally just 2 milliseconds which doesn’t even span to 2 frames in 60hz and so the optimization is so slim that it’s not faster at all times. In order to have a noticeable difference you would need to do that iteration 1 billion times which is ridiculous and you rarely need to do 1 billion iterations normally.
It is slower, read the post again and the order of the output and the print statements.
Lol the “faster method” is actually 0.002 seconds slower
What do you mean you literally said the one that localizes math.sine is faster
local tt = os.clock()
-- Faster
local sin = math.sin
for i = 1, 1000000 do
local x = sin(i)
end
print(os.clock()-tt)
Oops, forgot to remove that comment. I copied directly from your code, look at the output results, not the comments.
Any time you “save” (negligible) by localizing you will lose by doing object oriented design 10 times over. OOP is not a fast paradigm in languages like Lua and while it’s not noticeable it probably wastes orders of magnitude more time in calling methods than you will get back from localizing every single operation in a script.
Just stick to normal, readable code. If you want to optimize, look into algorithms and their time/space complexity when you write them.