User:Tekkub/Table profiling

local p = function(t) ChatFrame1:AddMessage(t) end

collectgarbage("stop")

for pow=6,9 do local limit = math.pow(2,pow) p("-- "..limit.." --")

collectgarbage("collect")

local mem, time local numt = 1000 local tables = {} local gcalloc = {} local v = "Blah" for i=1,limit do  gcalloc [i] = v  gcalloc [tostring(i)] = v end

-

for i=1,numt do  tables[i] = {} for j=1,limit do tables[i][j] = v end end

tables = {}

time, mem = GetTime, collectgarbage("count") collectgarbage("collect") p(string.format("GC array: %.03f sec %d B", GetTime-time, (collectgarbage("count")-mem)*1024))

--

for i=1,numt do  tables[i] = {} for j=1,limit do tables[i][j] = v end end

time, mem = GetTime, collectgarbage("count") for i=1,numt do  for j in pairs(tables[i]) do tables[i][j] = nil end end p(string.format("Erase array: %.03f sec %d B", GetTime-time, (collectgarbage("count")-mem)*1024))

--

for i=1,numt do  tables[i] = {} for j=1,limit do tables[i][j] = v end end

time, mem = GetTime, collectgarbage("count") for i=1,numt do  for j in pairs(tables[i]) do tables[i][j] = nil end tables[i].blah = v tables[i].blah = nil end p(string.format("Erase/shrink array: %.03f sec %d B", GetTime-time, (collectgarbage("count")-mem)*1024))

for i=1,numt do  tables[i] = {} for j=1,limit do tables[i][tostring(j)] = v end end collectgarbage("collect") tables = {}

time, mem = GetTime, collectgarbage("count") collectgarbage("collect") p(string.format("GC Hash: %.03f sec %d B", GetTime-time, (collectgarbage("count")-mem)*1024))

--

tables = {} for i=1,numt do  tables[i] = {} for j=1,limit do tables[i][tostring(j)] = v end end

time, mem = GetTime, collectgarbage("count") for i=1,numt do  for j in pairs(tables[i]) do tables[i][j] = nil end tables[i].blah = v tables[i].blah = nil end p(string.format("Erase Hash: %.03f sec %d B", GetTime-time, (collectgarbage("count")-mem)*1024))

end

Results:

-- 64 -- GC array: 0.054 sec -1076763 B Erase array: 0.032 sec  0 B Erase/shrink array: 0.037 sec  -984000 B GC Hash: 0.056 sec  -2612420 B Erase Hash: 0.033 sec  0 B -- 128 -- GC array: 0.052 sec -2100420 B Erase array: 0.064 sec  0 B Erase/shrink array: 0.067 sec  -2008000 B GC Hash: 0.058 sec  -5172420 B Erase Hash: 0.066 sec  0 B -- 256 -- GC array: 0.058 sec -4148420 B Erase array: 0.133 sec  0 B Erase/shrink array: 0.137 sec  -4056000 B GC Hash: 0.063 sec  -10292420 B Erase Hash: 0.134 sec  0 B -- 512 -- GC array: 0.060 sec -8244420 B Erase array: 0.286 sec  -52 B Erase/shrink array: 0.268 sec  -8152000 B GC Hash: 0.069 sec  -20532420 B Erase Hash: 0.266 sec  0 B