Good vs. Evil

  • The FTB Forum is now read-only, and is here as an archive. To participate in our community discussions, please join our Discord! https://ftb.team/discord

lenscas

Over-Achiever
Jul 31, 2013
2,015
1,801
248
78 well, I just found a function inside lua that I have no idea about why it exists.
It is the function table.setn()
Code:
local someTable={1,2}
print(#someTable)--this will print 2
table.insert(someTable,4)
print(#someTable)--it will now print 3
table.set(someTable,20)
print(#someTable)--it should now print 20

Note, I haven't tested this code yet but if the documentation is right it should work like this
 

lenscas

Over-Achiever
Jul 31, 2013
2,015
1,801
248
78 it sets the size of a table, which doesn't do a whole lot as you can expand a table in lua just by inserting new things in it.
Doing this also breaks table.remove(), so...that is nice I guess?
 

Someone Else 37

Forum Addict
Feb 10, 2013
1,876
1,440
168
78 @lenscas I'm guessing that it's for optimization purposes. Say you want to generate one million random numbers and store them in a table. You could make a for loop and expand the table on each iteration (which is slow), or you could go ahead and allocate the whole table and fill it in as you go (much faster).
 

lenscas

Over-Achiever
Jul 31, 2013
2,015
1,801
248
78 @lenscas I'm guessing that it's for optimization purposes. Say you want to generate one million random numbers and store them in a table. You could make a for loop and expand the table on each iteration (which is slow), or you could go ahead and allocate the whole table and fill it in as you go (much faster).
79 shouldn't matter I would think. In lua all variables are by default nil (or NULL) even those that don't exist yet.
Also, table.remove() breaks because it uses the # thing to get the last entry (according to the documentation).
 

Someone Else 37

Forum Addict
Feb 10, 2013
1,876
1,440
168
80
79 shouldn't matter I would think. In lua all variables are by default nil (or NULL) even those that don't exist yet.
Also, table.remove() breaks because it uses the # thing to get the last entry (according to the documentation).
It's the act of growing an array that's slow. I don't know what language Lua is written in, but at some level, it's going to need to deal with binary data stored in computer memory. If LUA tables are based on, for instance, arrays in C (which have fixed size), then whenever the array fills up, LUA would need to create a whole new array somewhere else in memory and copy all the data into it. And as that will occur every single time the current array fills up, it will happen a *lot*.

C does have a shortcut for this behavior, which is its realloc(array, newSize) function. Realloc will first check to see if there is enough available space in memory after the given array to accommodate the requested amount of additional space. If so, it'll just use it; if not, realloc will automatically create a new array that is big enough somewhere in memory where there's more space and copy everything over for you.

But that's still slower than just allocating as much space as you needed in the first place.

I suggest you try fixing up these two quasi-LUA scripts (because I don't remember how everything works in LUA, so they probably won't work as is) and comparing how long they take to run.
Code:
local time = system.time
local table = {}
for i from 1 to 1000000 do
    table[i] = i
end
print(system.time - time)
Code:
local time = system.time
local table = {}
table.set(table, 1000000)
for i from 1 to 1000000 do
    table[i] = i
end
print(system.time - time)
I guarantee you the second will run faster. Unless LUA tables are linked lists, which seems dumb since they can't be random-accessed.
 

lenscas

Over-Achiever
Jul 31, 2013
2,015
1,801
248
83 @Someone Else 37
I tried to run the following code, just to see how the different ways to insert into a table would change the speed.
(btw os.clock() counts the time the program is running os.time() is used to get the system time. For as far as I know, os.clock() is recommended when trying to debug speed problems )

Code:
local time =  os.clock()
local tableT = {}
for i= 1 , 1000000 do
    tableT[i] = i
end
print(os.clock() - time)
local newTime=os.clock()

local table2 = {}
table.setn(table2, 1000000)
for w = 1, 1000000 do
    table2[i] = i
end

print(os.clock() - newTime)
local newTime3=os.clock()
local table3 = {}
for z= 1, 1000000 do
    table.insert(table3,i)
end
print(os.clock()-newTime3)

local newTime4=os.clock()
local table4 ={}
table.setn(table4,1000000)
for x=1,1000000 do
    table.insert(table4,x)
end
print(os.clock()-newTime4)

This is what I got:

lenscas@Lenscas-desktop-1:~/luaStuff$ lua testsetn.lua
0.020737
lua: testsetn.lua:10: 'setn' is obsolete
stack traceback:
[C]: in function 'setn'
testsetn.lua:10: in main chunk
[C]: ?

Will try without the setn soon

edit: it looks like table.insert() is actually slower then table3. It kind of makes sense.
 

Someone Else 37

Forum Addict
Feb 10, 2013
1,876
1,440
168
84
83 @Someone Else 37
I tried to run the following code, just to see how the different ways to insert into a table would change the speed.
(btw os.clock() counts the time the program is running os.time() is used to get the system time. For as far as I know, os.clock() is recommended when trying to debug speed problems )

Code:
local time =  os.clock()
local tableT = {}
for i= 1 , 1000000 do
    tableT[i] = i
end
print(os.clock() - time)
local newTime=os.clock()

local table2 = {}
table.setn(table2, 1000000)
for w = 1, 1000000 do
    table2[i] = i
end

print(os.clock() - newTime)
local newTime3=os.clock()
local table3 = {}
for z= 1, 1000000 do
    table.insert(table3,i)
end
print(os.clock()-newTime3)

local newTime4=os.clock()
local table4 ={}
table.setn(table4,1000000)
for x=1,1000000 do
    table.insert(table4,x)
end
print(os.clock()-newTime4)

This is what I got:

lenscas@Lenscas-desktop-1:~/luaStuff$ lua testsetn.lua
0.020737
lua: testsetn.lua:10: 'setn' is obsolete
stack traceback:
[C]: in function 'setn'
testsetn.lua:10: in main chunk
[C]: ?

Will try without the setn soon
*scratches head*
 

lenscas

Over-Achiever
Jul 31, 2013
2,015
1,801
248
85 well, apparently we now need to make the function ourself if we want to use it. In lua 5.2 you do it apparently like this
function setn(t,n)
setmetatable(t,{__len=function() return n end})
end