Code Optimizing & Performance trics

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.
User avatar
Virox
Citizen
Posts: 64
Joined: Thu Jan 07, 2010 6:24 pm

Code Optimizing & Performance trics

Post 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:
User avatar
kalle2990
Party member
Posts: 245
Joined: Sat Sep 12, 2009 1:17 pm
Location: Sweden

Re: Code Optimizing & Performance trics

Post by kalle2990 »

Not loading anything that's repeated every frame in the main loop (aka love.run) :)
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: Code Optimizing & Performance trics

Post by Robin »

Disabling modules you aren't using might help.
Help us help you: attach a .love.
User avatar
bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

Re: Code Optimizing & Performance trics

Post 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
User avatar
Virox
Citizen
Posts: 64
Joined: Thu Jan 07, 2010 6:24 pm

Re: Code Optimizing & Performance trics

Post 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 :)
User avatar
bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

Re: Code Optimizing & Performance trics

Post 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).
User avatar
Virox
Citizen
Posts: 64
Joined: Thu Jan 07, 2010 6:24 pm

Re: Code Optimizing & Performance trics

Post 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
User avatar
kalle2990
Party member
Posts: 245
Joined: Sat Sep 12, 2009 1:17 pm
Location: Sweden

Re: Code Optimizing & Performance trics

Post 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...
User avatar
bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

Re: Code Optimizing & Performance trics

Post by bartbes »

Wut?! The lua GC runs automatically.
User avatar
kalle2990
Party member
Posts: 245
Joined: Sat Sep 12, 2009 1:17 pm
Location: Sweden

Re: Code Optimizing & Performance trics

Post 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?
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot] and 140 guests