## Questions about Lua Optimization

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Zorbatron
Citizen
Posts: 78
Joined: Wed May 27, 2009 6:58 pm

### Questions about Lua Optimization

Before you consider this question, understand I am asking in the pure sense of optimization and runtime execution time, disregard the value of readibility or modular code because I understand the uses of both I just want to understand something about speed.

Question 1: Is it faster to execute a function with alot of code to perform a task, or have a series of functions with less code to preform the same task collectivly.

My understanding of how Lua works would tell me having a single function would be better because lua wouldn't need to pass variables and create new function stacks. However, maybe a single large function may have a very large stack because of all the local variables and maybe having many small functions inside it would lessen the stack and run faster? Maybe both methods are basically the same?

More questions.

Question 2: Is it faster to store string concatinations in locals if they are used multiple times, or is it faster to just reconcat them each time.

Example:

Code: Select all

function yo(var)
print("sup"..var);
print("sup"..var);
print("sup"..var);
print("sup"..var);
end

function yo(var)
local str = "sup"..var;
print(str);
print(str);
print(str);
print(str);
end

I'm really not sure about this one, it sounds like making a local is better, but I don't know how fast stack pushing is compared to string concating. I know for a fact that if the string where massive, say var:length() > 512, I would definantly use a local. I'm pretty sure that reconcatting the strings like in the first function would cause lua to allocate memory for all 4 concatinations regardless of them all being the same string.

Kuromeku
Party member
Posts: 166
Joined: Sun Jul 20, 2008 5:45 pm

### Re: Questions about Lua Optimization

That is very trivial optimisation.

bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

### Re: Questions about Lua Optimization

1: if using the same amount of code the lookups themselves can make a difference though probably tiny however this is no excuse for code duplication

2: locals are fast in lua

disclaimer: i cant present these as facts this is experience and a few articles

TacticalPenguin
Prole
Posts: 15
Joined: Thu Dec 11, 2008 5:44 am

### Re: Questions about Lua Optimization

part 1 I would lean towards a big function - lua's local variables are relatively very fast, if you dont need the splitup functions to get the job done and you wont be duplicating code beyond reason, keep it to one function. Part 2 go with a relaxed rule of 3 based mostly on string size - but as someone else said these are trivial optimizations and I would just stick to however your coding style goes rather than what may be more efficient, you'd need thousands of repetitions to measure a speed difference.

Zorbatron
Citizen
Posts: 78
Joined: Wed May 27, 2009 6:58 pm

### Re: Questions about Lua Optimization

Thanks for the replies, its good to get some more opinions.
Kuromeku wrote:That is very trivial optimisation.
Not at all, what if the string contains an entire file? Say 10kb? It would be far less trivial then.

### Who is online

Users browsing this forum: No registered users and 71 guests