Local what's the best way ?

Hi,

First, sorry for the format of my snippets, i’m on phone.
My question is what’s the best way between this two examples or is the same ?

local t={}
t.head=display.newCircle(10,10,10)

Or

local t={}
local head=display.newCircle(10,10,10)
t.head=head

first, no need to create temporary local

or even better, more efficient:

local t = { head = display.newCircle(10,10,10) }

Note: Any Lua experts out there?  This may not be as efficient as it looks at first.  I’d love to hear an expert weigh in.

without claiming to be an expert, i can at least suggest that there’s no practical difference between:

local t = {} t.foo = "bar"

local t = { foo = "bar" }

as they’ll both compile to the same pair of newtable + settable opcodes.

@davebollinger @ roaminggamer

The second one is definetly faster (but not by much).

LUA tables are hashed in relation to there entries. (0 entries, 1 entry, 2 entries, 4 etries, 8 entries, 16 entries etc.)

Each time a LUA table exeeds there current hash maximum the whole table is rehashed to the next power of two.

(e.g. a table has 8 entries and a ninth is inserted, so the table is rehashed to 16 entries)

Rehashing takes processing time, so fewer rehashes are more efficient.

So it is faster to insert elements directly into a table at its creation than afterwards.

@espace3d

You only need to localize the value i you want to use it later on. (so you do not need to acces the table again.

As I stated above, inserting values at a tables creation is faster. So if possible use one of the following the following:

local t = { head = display.newCircle(10,10,10), }

local head = display.newCircle(10,10,10) local t = { head = head }

Greetings

Torben :slight_smile:

>> Rehashing takes processing time, so fewer rehashes are more efficient.

conceded, tho it would take far more than this to notice any practical difference.

bowing out now, cuz i know where “optimization” questions always lead… :smiley:

hi,

Thanks for your advices :slight_smile:

Now i write my table like your solutions.

it’s curious that the majority of people write table like my first snippet and not like the optimized way.

local t={ head=display.newCircle(10,10,10) }

there must be a page in the documentation or in the blog about that…

Here’s a great general LUA optimization document.

The tables part starts at page 5.

https://www.lua.org/gems/sample.pdf

thanks it looks very interesting

first, no need to create temporary local

or even better, more efficient:

local t = { head = display.newCircle(10,10,10) }

Note: Any Lua experts out there?  This may not be as efficient as it looks at first.  I’d love to hear an expert weigh in.

without claiming to be an expert, i can at least suggest that there’s no practical difference between:

local t = {} t.foo = "bar"

local t = { foo = "bar" }

as they’ll both compile to the same pair of newtable + settable opcodes.

@davebollinger @ roaminggamer

The second one is definetly faster (but not by much).

LUA tables are hashed in relation to there entries. (0 entries, 1 entry, 2 entries, 4 etries, 8 entries, 16 entries etc.)

Each time a LUA table exeeds there current hash maximum the whole table is rehashed to the next power of two.

(e.g. a table has 8 entries and a ninth is inserted, so the table is rehashed to 16 entries)

Rehashing takes processing time, so fewer rehashes are more efficient.

So it is faster to insert elements directly into a table at its creation than afterwards.

@espace3d

You only need to localize the value i you want to use it later on. (so you do not need to acces the table again.

As I stated above, inserting values at a tables creation is faster. So if possible use one of the following the following:

local t = { head = display.newCircle(10,10,10), }

local head = display.newCircle(10,10,10) local t = { head = head }

Greetings

Torben :slight_smile:

>> Rehashing takes processing time, so fewer rehashes are more efficient.

conceded, tho it would take far more than this to notice any practical difference.

bowing out now, cuz i know where “optimization” questions always lead… :smiley:

hi,

Thanks for your advices :slight_smile:

Now i write my table like your solutions.

it’s curious that the majority of people write table like my first snippet and not like the optimized way.

local t={ head=display.newCircle(10,10,10) }

there must be a page in the documentation or in the blog about that…

Here’s a great general LUA optimization document.

The tables part starts at page 5.

https://www.lua.org/gems/sample.pdf

thanks it looks very interesting