Page 1 of 4

Lua Performance Tips

Posted: Thu Aug 11, 2011 9:54 am
by Roland_Yonaba
This is for all those for have been looking for techniques to improve their coding style, and all those who want to learn.
This is a part of LuaGems, more precisely chapter two.
Guess it might be useful to you. I was, to me.

Link: Lua Performance Tips, By Roberto Ierusamlischy.

Re: Lua Performance Tips

Posted: Thu Aug 11, 2011 11:45 am
by Taehl
This is another good page to read: http://trac.caspring.org/wiki/LuaPerformance

Re: Lua Performance Tips

Posted: Thu Aug 11, 2011 11:52 am
by T-Bone
I only looked through the first one there quickly, and was really surprised that

Code: Select all

local sin = math.sin
many times sin(x)
is faster than

Code: Select all

many times math.sin(x)
Is this true for love functions as well? Should you like do

Code: Select all

local print = love.graphics.print
and so on? I already do that with some functions, but only when I want to change them.

Re: Lua Performance Tips

Posted: Thu Aug 11, 2011 12:03 pm
by bartbes
Yes.

Re: Lua Performance Tips

Posted: Thu Aug 11, 2011 12:20 pm
by Roland_Yonaba
T-Bone wrote:Is this true for love functions as well?
Of course, it is... Just assume that love functions are packed in a global table. Assigning them to local function make them run faster.
Anyway, I am pretty sure that this is unecessary, cause Löve engine is fast enough. That would be superfluous...

@Taehl: Thanks for that link!

Re: Lua Performance Tips

Posted: Thu Aug 11, 2011 12:32 pm
by Taehl
T-Bone, the basic idea is that Lua can look up local variables (and functions are variables in Lua) faster than it can global variables.

Re: Lua Performance Tips

Posted: Thu Aug 11, 2011 1:18 pm
by ivan

Code: Select all

local print = love.graphics.print()
I think you mean:

Code: Select all

local print = love.graphics.print
The biggest performance drop in Lua that I've noticed is from creating unnecessary tables (for example in a loop) that get destroyed right away.

Re: Lua Performance Tips

Posted: Thu Aug 11, 2011 1:22 pm
by T-Bone
I understand. It's faster but not so fast that you have to care unless you're doing lots of calls to the same function in one specific place.

Re: Lua Performance Tips

Posted: Thu Aug 11, 2011 1:27 pm
by ivan
T-Bone wrote:I understand. It's faster but not so fast that you have to care unless you're doing lots of calls to the same function in one specific place.
Yep, for example:

Code: Select all

for i = 1, 100 do
  love.graphics.print(i, i*10, 10)
end
Would be slower than:

Code: Select all

local print = love.graphics.print
for i = 1, 100 do
  print(i, i*10, 10)
end

Re: Lua Performance Tips

Posted: Thu Aug 11, 2011 3:19 pm
by vrld
The speed difference would still not be noticeable.

What the author writes on the first page is the most important part of the whole article:
We should not try to optimize software without proper measurements.
LuaProfiler is great for that.

Also note that you will probably get better results if you try to reduce the overall complexity and optimize your algorithms (save intermediate results, preprocess data, divide and conquer, math, that sort of stuff) before you go on to micro-optimizations like the ones mentioned in the article.

And above all:
Bruce Whiteside wrote:Make it work before you make it work fast.