Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function test_tableremove(mylist)
- for i=#mylist,1,-1 do
- if mylist[i] == 13 then
- table.remove(mylist, i)
- end
- end
- end -- func
- function test_mitch(mylist)
- -- NOTE: This is a messy, hardcoded version of my algorithm, for test-purposes.
- -- Go to https://stackoverflow.com/a/53038524/8874388 for the actual, clean version.
- local j, n = 1, #mylist;
- for i=1,n do
- local value = mylist[i]
- if value ~= 13 then -- Keep everything except "13"
- -- Move i's kept value to j's position, if it's not already there.
- if (i ~= j) then
- mylist[j] = mylist[i];
- mylist[i] = nil;
- end
- j = j + 1; -- Increment position of where we'll place the next kept value.
- else -- Delete "13"
- mylist[i] = nil;
- end
- end
- end
- function build_tables()
- local tables = {}
- for i=1, 10 do
- tables[i] = {}
- for j=1, 2000000 do -- 2 million items
- tables[i][j] = (j % 5 == 0 and 13 or 100) -- put a "13" (deleted) in every 5th item, otherwise "100" (kept)
- end
- end
- return tables
- end
- function time_func(func, name)
- local tables = build_tables()
- time0 = os.clock()
- for i=1, #tables do
- func(tables[i])
- end
- time1 = os.clock()
- print(string.format("[%10s] elapsed time: %.3f\n", name, time1 - time0))
- end
- time_func(test_mitch, "mitch")
- time_func(test_tableremove, "table.remove")
- time_func(test_mitch, "mitch")
- time_func(test_tableremove, "table.remove")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement