WoW:API tinsert: Difference between revisions

m
Move page script moved page API tinsert to WoW:API tinsert without leaving a redirect
No edit summary
 
m (Move page script moved page API tinsert to WoW:API tinsert without leaving a redirect)
 
(3 intermediate revisions by 3 users not shown)
Line 1: Line 1:
{{luaapi}}
From [http://lua-users.org/wiki/TableLibraryTutorial TableLibraryTutorial] of lua-users.org.
From [http://lua-users.org/wiki/TableLibraryTutorial TableLibraryTutorial] of lua-users.org.


  table.insert(table, [pos,] value)
  table.insert(table, [pos,] value)
tinsert(table[, pos], value)


Insert a given value into a table. If a position is given insert the value before the element currently at that position:
Insert a given value into a table. If a position is given insert the value before the element currently at that position:
Line 46: Line 48:
  11      end
  11      end
  n      11
  n      11
== Speed ==
If you are appending to an integer-indexed table, it is always faster to track the size yourself, as this example demonstrates (clocked on a 3Ghz P4)
local t = {}
local tinsert=table.insert
local b = os.clock()
local n=1
t.n=1
for i=1,5e6 do
table.insert(t, "")  -- 2.25s - extra cost of looking up table. subkeys
tinsert(t, "")      -- 2.02s - local function call is slightly faster
t[#t+1] = ""        -- 1.72s - a lot of the cost seems to be #t
local tn=t.n; t[tn]=""; t.n=tn+1  -- 1.19s - storing "n" in the table
t[n]="";n=n+1        -- 0.88s - storing "n" in a local
t[i]=""              -- 0.78s - cheat, we don't have "i" usually
-- empty  -- 0.08s overhead for the loop
end
local e = os.clock()
print(e-b)
== See Also ==
* [[tinsertbeforeval]]
Anonymous user