I heard in vanilla lua it’s way better to call a function from local reference like this:
local math_cos = math.cos
while true do
print(math_cos(3.14))
end
I wonder if it’s still the same in luau, I know C++ compilers can see repetitive use of the same object function and rather parse it as a pointer without calling the base object.
local pre = os.clock()
for i = 1, 45000000 do
math.cos(math.pi/5)
end
for i = 1, 45000000 do
math.cos(math.pi/9)
end
for i = 1, 45000000 do
math.cos(math.pi/13)
end
for i = 1, 45000000 do
math.cos(math.pi/17)
end
local post = os.clock()
-----------------
local pre2 = os.clock()
local math_cos = math.cos
for i = 1, 45000000 do
math_cos(math.pi/5)
end
for i = 1, 45000000 do
math_cos(math.pi/9)
end
for i = 1, 45000000 do
math_cos(math.pi/13)
end
for i = 1, 45000000 do
math_cos(math.pi/17)
end
local post2 = os.clock()
print("Normal:", post-pre)
print("Var___:", post2-pre2)
As can be seen, the 2 times, after 180million trials each, is almost identical. This was tested both in studio and in game.
btw if anyone else looks at this topic, don’t misunderstand that you should still reference math calculations just not functions like math.cos.
Using the same format but with math.pi/5
local pre = workspace:GetServerTimeNow()
for i = 1, 450000000 do
math.cos(math.pi/5)
end
local post = workspace:GetServerTimeNow()
-----------------
local pre2 = workspace:GetServerTimeNow()
local math_pi = math.pi/5
for i = 1, 450000000 do
math.cos(math_pi)
end
local post2 = workspace:GetServerTimeNow()
print("Local_Normal:", post-pre)
print("Local_Var___:", post2-pre2)
You’ll see that referencing actual math calculations before you use them actually helps greatly in performance especially if you’re gonna use the same one multiple times.
He wasn’t spreading misinformation and my explanation wasn’t any better just a different out look @towerscripter1386@RaterixRGL , he addressed the actual question and I wanted to chip in just in case someone got confused by the differences.
I still would prefer to consider your explanation a bit better since you not only said that there is no difference between referencing and calling a function but also provided an example where storing something in a variable actually worth it
Realistically, the performance impact here is negligible. You shouldn’t be considering the performance of small things like this.
It’s called premature optimisation, and in the words of Tony Hoare, “premature optimisation is the root of all evil”.
Are you going to be running a simple math operation 450,000,000 times?
For commercial projects, yes premature optimization is pretty bad. But projects which are complex and require a well-optimized approach from the start no
As programmers is it very rare that something so incremental as this (say, a 2x improvement) is actually the primary cause of performance issues.
There’s this concept of Big O Notation in computer science that acts as a way to measure the performance of an algorithm when compared to the size of the input.
Let’s give a practical example:
You have two list sorting algorithms. Algorithm A has a Big O of ln(x). Algorithm B has a Big O of x^2. Let’s say you give these algorithms the same list of 100 unordered items.
Algorithm A would have to perform ln(100) comparisons, which is about 5 comparisons total.
Algorithm B would have to perform 100^2 comparisons, which is 10,000 comparisons.
Algorithm A runs, on average, 2,000 times faster than Algorithm B.
This is to say nothing of the fact that Algorithm A would probably take 10-15x longer to execute once than the time savings of adopting an optimisation like this.
This is why minute performance improvements like this are rarely going to help matters. Speaking as a professional game developer in his final year of university, I would be shocked if you were to ever find a performance issue that could be fixed by modifying such a simple thing in your entire life.