The table.search function provided here is updated and much more optimized, (based on my test in that post, using very same laptop model, or desktop using i7-4790k @4GHz).
It seems that table.search is 2-3 times much faster, thus this function could be handy almost in any situation, and I really hope you guys make use of it.
I commented new timestamps, along with the older previous test ones.
Code: Select all
-- i7-4720HQ @3.5GHz 4Cores
local start_time = os.clock();
t = {}
for i=1,500000 do --500k table size
t[i]=i-1
end
print(os.clock()-start_time) -- 0.0280000.. -- 0.05 sec to create the table
local start_time = os.clock();
print(tostring(table.search(t,500000,false,true)))--true -- will try to find a key with the value of 500,000
print(os.clock()-start_time) -- 0.08799999.. -- 0.197sec sec to scan the whole table
function nestedTable(object,times) -- creates nested table -> object={{{{..n times}}}}
if (times > 0 ) then
object[#object+1] = {times = times}
return (nestedTable(object[#object],times-1))
end
end
local start_time = os.clock();
t = {};
nestedTable(t,15000) --> will create table inside a table x 15000 times.
print(os.clock()-start_time) -- 0.017000 .. --0.007 sec to create the nested table
local start_time = os.clock();
print(tostring(table.search(t,1,false)))-- true -- will try to find a 1 (as a table value), the very last table value
print(os.clock()-start_time) -- 0.04199999.. -- 0.014 sec to find the value in the nested table
I recommend using the code as it is, It's very simple, basically iterates over the whole table and it's childs childs childs...