Lua Performance Tips

General discussion about LÖVE, Lua, game development, puns, and unicorns.
User avatar
Roland_Yonaba
Inner party member
Posts: 1563
Joined: Tue Jun 21, 2011 6:08 pm
Location: Ouagadougou (Burkina Faso)
Contact:

Lua Performance Tips

Post 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.
User avatar
Taehl
Dreaming in associative arrays
Posts: 1025
Joined: Mon Jan 11, 2010 5:07 am
Location: CA, USA
Contact:

Re: Lua Performance Tips

Post by Taehl »

This is another good page to read: http://trac.caspring.org/wiki/LuaPerformance
Earliest Love2D supporter who can't Love anymore. Let me disable pixel shaders if I don't use them, dammit!
Lenovo Thinkpad X60 Tablet, built like a tank. But not fancy enough for Love2D 0.10.0+.
User avatar
T-Bone
Inner party member
Posts: 1492
Joined: Thu Jun 09, 2011 9:03 am

Re: Lua Performance Tips

Post 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.
Last edited by T-Bone on Thu Aug 11, 2011 1:22 pm, edited 1 time in total.
User avatar
bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

Re: Lua Performance Tips

Post by bartbes »

Yes.
User avatar
Roland_Yonaba
Inner party member
Posts: 1563
Joined: Tue Jun 21, 2011 6:08 pm
Location: Ouagadougou (Burkina Faso)
Contact:

Re: Lua Performance Tips

Post 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!
User avatar
Taehl
Dreaming in associative arrays
Posts: 1025
Joined: Mon Jan 11, 2010 5:07 am
Location: CA, USA
Contact:

Re: Lua Performance Tips

Post 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.
Earliest Love2D supporter who can't Love anymore. Let me disable pixel shaders if I don't use them, dammit!
Lenovo Thinkpad X60 Tablet, built like a tank. But not fancy enough for Love2D 0.10.0+.
User avatar
ivan
Party member
Posts: 1915
Joined: Fri Mar 07, 2008 1:39 pm
Contact:

Re: Lua Performance Tips

Post 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.
User avatar
T-Bone
Inner party member
Posts: 1492
Joined: Thu Jun 09, 2011 9:03 am

Re: Lua Performance Tips

Post 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.
User avatar
ivan
Party member
Posts: 1915
Joined: Fri Mar 07, 2008 1:39 pm
Contact:

Re: Lua Performance Tips

Post 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
User avatar
vrld
Party member
Posts: 917
Joined: Sun Apr 04, 2010 9:14 pm
Location: Germany
Contact:

Re: Lua Performance Tips

Post 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.
I have come here to chew bubblegum and kick ass... and I'm all out of bubblegum.

hump | HC | SUIT | moonshine
Post Reply

Who is online

Users browsing this forum: No registered users and 4 guests