Is it worth storing a reference to a function?

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.

3 Likes

Yes, storing the function is useful and performant.

You can also temporarily store it if you only need it for a short period with do end groups.

1 Like

It has absolutely zero impact on performance.

I used the following code to test:

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)

image
As can be seen, the 2 times, after 180million trials each, is almost identical. This was tested both in studio and in game.

3 Likes

Good to know roblox is taking the same steps as C++ did, ty

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)

image

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.

3 Likes

Caching some systems, such as meta functions of a module script still benefit greatly. Please don’t spread misinformation.

Correction is the way to go, ty for doing a better explanation

I was addressing the actual question provided in the original post with their provided example. Nothing I said was misinformation.

2 Likes

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?

Probably not.
I’d explain further, but Code Aesthetic does a much better job of explaining it than I ever could: https://youtu.be/tKbV6BpH-C8?si=KHr0_B7AZcuwdd1e

1 Like

I disagree with this mentality.

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.

It’s not about performance issues, it’s about bleeding edge absolute best possible performance.

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