Page 1 of 2

Code Optimizing & Performance trics

Posted: Sat Feb 27, 2010 10:41 am
by Virox
I didn't find a topic grouping several issues, tips, trics to make your code as optimized as possible and achieve a better performance.
Things you should bear in mind when making your game a fast as possible. Like how to handle variables, loops, tables, garbage collection...

These are things I've found already:


Use as much locals as possible and reuse them, turn globals into locals

Code: Select all

WRONG WAY
for i=1,1000000 do
   localx = math.sin(i)
end

RIGHT WAY
local sin = math.sin
for i=1, 1000000 do
   localx = sin(i)
end

love.timer.getFPS()
seems to lower performance on itsself by using it.


When using mapscrolling or simular things you shouldn't draw things which are outside the visible windowscreen.


Are there other tips people and myself should know about? :shock:

Re: Code Optimizing & Performance trics

Posted: Sat Feb 27, 2010 10:44 am
by kalle2990
Not loading anything that's repeated every frame in the main loop (aka love.run) :)

Re: Code Optimizing & Performance trics

Posted: Sat Feb 27, 2010 11:00 am
by Robin
Disabling modules you aren't using might help.

Re: Code Optimizing & Performance trics

Posted: Sat Feb 27, 2010 11:00 am
by bartbes
Caching resources. (that might be what kalle meant, not sure)
Prevent as much table lookups as possible.
math.floor is pretty slow. (and ceil, probably as well)
In lines that get called more than once don't use anonymous functions.
Use a minimum amount of loops, they aren't particularly fast, so try to keep them as short as possible and not to use them when there's a good alternative.

probably some others I forgot atm

Re: Code Optimizing & Performance trics

Posted: Sat Feb 27, 2010 3:14 pm
by Virox
bartbes wrote:Prevent as much table lookups as possible.
do you mean loop through a table using pairs or table?

bartbes wrote:In lines that get called more than once don't use anonymous functions

I have no clue about what you are talking about, anonymous functions?

Keep those tips coming :)

Re: Code Optimizing & Performance trics

Posted: Sat Feb 27, 2010 3:36 pm
by bartbes
I mean table (or really anything you look something up in a table, hence the name lookup)
And anonymous functions are functions without names, you know.. like this:

Code: Select all

table.sort(t, function(a, b) return a.x < b.x end)
the reason is very simple, every time you do it it not only creates the function, it also pushes extra data to the stack (upvalues, etc).

Re: Code Optimizing & Performance trics

Posted: Sun Feb 28, 2010 10:40 am
by Virox
Thx for the explanation.

I've found a great page where they test several lua code performances.
http://trac.caspring.org/wiki/LuaPerformance

Re: Code Optimizing & Performance trics

Posted: Sun Feb 28, 2010 1:46 pm
by kalle2990
Don't use locals in the main loop (aka love.run) without using a garbagecollecter too... :? It will slowly increase the RAM usage...

Re: Code Optimizing & Performance trics

Posted: Sun Feb 28, 2010 1:55 pm
by bartbes
Wut?! The lua GC runs automatically.

Re: Code Optimizing & Performance trics

Posted: Sun Feb 28, 2010 2:09 pm
by kalle2990
Oh.. I just saw a leaning curve of RAM usage on LÖVE, I didn't check for a long time though. How long is it between the GC's?