Page 1 of 1

Many people will know this, but some don't..

Posted: Fri Aug 26, 2011 8:22 pm
by GijsB
When you set love.graphics(or even love.grahpics.draw) to some variable, and then draw things the speed increases with 30% :ultrashocked:

Re: Many people will know this, but some don't..

Posted: Fri Aug 26, 2011 8:32 pm
by vrld
GijsB wrote:speed increases with 30%
[citation needed]

Re: Many people will know this, but some don't..

Posted: Fri Aug 26, 2011 8:41 pm
by ivan
GijsB wrote:When you set love.graphics(or even love.grahpics.draw) to some variable, and then draw things the speed increases with 30% :ultrashocked:
Sure, every time you use the "." operator you are doing a table lookup.
If you are using love.graphics.draw in a loop a total of 1000 times you are doing 2 lookups * 1000.
However if you store a local reference (local d = love.graphics.draw) the total number of lookups is fewer.
There was a topic about this already:
http://love2d.org/forums/viewtopic.php?f=3&t=3500

Re: Many people will know this, but some don't..

Posted: Sat Aug 27, 2011 12:41 am
by slime
ivan wrote:
GijsB wrote:When you set love.graphics(or even love.grahpics.draw) to some variable, and then draw things the speed increases with 30% :ultrashocked:
Sure, every time you use the "." operator you are doing a table lookup.
If you are using love.graphics.draw in a loop a total of 1000 times you are doing 2 lookups * 1000.
However if you store a local reference (local d = love.graphics.draw) the total number of lookups is fewer.
There was a topic about this already:
http://love2d.org/forums/viewtopic.php?f=3&t=3500
The performance increase will not even be close to 30% though, because the vast majority of performance loss from love.graphics.draw is the C++ side stuff and the image being drawn, not the function call, which was vrld's point.

Re: Many people will know this, but some don't..

Posted: Sat Aug 27, 2011 4:50 am
by miloguy
So my

Code: Select all

local g=love.graphics
at the top of all my games actually benefits more that just letting me not type as much?
Cool :D

Re: Many people will know this, but some don't..

Posted: Sat Aug 27, 2011 6:15 pm
by Robin
miloguy wrote:at the top of all my games actually benefits more that just letting me not type as much?
Yes, you can confuse the hell out of people reading your source (including future!you).

Oh, you were talking about benefits? ;)

Re: Many people will know this, but some don't..

Posted: Sat Aug 27, 2011 6:35 pm
by GijsB
Robin,

lovegraphics = love.graphics
(or lovegraphicsdraw = love.graphicsd.raw)

:joker:

edit :

i mean =
local lovegraphics = love.graphics
(local lovegraphicsdraw = love.graphicsd.raw)

Re: Many people will know this, but some don't..

Posted: Sat Aug 27, 2011 6:40 pm
by slime
GijsB wrote:Robin,

lovegraphics = love.graphics
(or lovegraphicsdraw = love.graphicsd.raw)



:joker:
Without the 'local' keyword it's still accessing the global stack, which will not increase performance very much at all, and again the vast majority of CPU time spent when calling love.graphics.draw is doing the C++ and GPU side stuff to actually draw the image, rather than the Lua call.

Re: Many people will know this, but some don't..

Posted: Sat Aug 27, 2011 7:22 pm
by GijsB
slime,

my bad, i forgot the typing local...

Re: Many people will know this, but some don't..

Posted: Mon Aug 29, 2011 4:47 pm
by T-Bone
This should be done in specific situations where many calls to love.graphics.draw are made, and then the local variables should be declared in the smallest possible scope. However, if you're going to do lots of love.graphics.draw in one place, you should probably draw it to a framebuffer and draw that afterwards anyway (depends on the situation of course).