Y
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
79 shouldn't matter I would think. In lua all variables are by default nil (or NULL) even those that don't exist yet.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).
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*.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).
local time = system.time
local table = {}
for i from 1 to 1000000 do
table[i] = i
end
print(system.time - time)
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)
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)
*scratches head*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