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(howMany)
- local ofs = {}
- for i=1,howMany do
- ofs[i*50] = true;
- end
- local tables = {}
- for i=1, 1 do
- tables[i] = {}
- for j=1, 2000000 do -- 2 million items
- tables[i][j] = (ofs[j] and 13 or 100) -- put a "13" (deleted) in the slots to be deleted, otherwise "100" (kept)
- end
- end
- return tables
- end
- function time_func(func, name, howMany)
- local tables = build_tables(howMany)
- time0 = os.clock()
- for i=1, #tables do
- func(tables[i])
- end
- time1 = os.clock()
- print(string.format("[%13s] elapsed time (deleting %.0f items): %.3f\n", name, howMany, time1 - time0))
- end
- -- The number below (i) controls how many items that will need to be deleted from the array.
- -- This test checks if table.remove() is ever faster if you only need to remove a few entries.
- -- The test arrays contain 2 million items, and the items that need to be removed at all at the start,
- -- hence triggering the need to re-index the subsequent array entries.
- for i=1,6 do
- time_func(test_mitch, "mitch", i)
- time_func(test_tableremove, "table.remove", i)
- end
- --[[
- LuaJIT 2.0.5 -- Copyright (C) 2005-2017 Mike Pall. http://luajit.org/
- JIT: ON CMOV SSE2 SSE3 SSE4.1 fold cse dce fwd dse narrow loop abc sink fuse
- [ mitch] elapsed time (deleting 1 items): 0.008
- [ table.remove] elapsed time (deleting 1 items): 0.010
- [ mitch] elapsed time (deleting 2 items): 0.008
- [ table.remove] elapsed time (deleting 2 items): 0.019
- [ mitch] elapsed time (deleting 3 items): 0.008
- [ table.remove] elapsed time (deleting 3 items): 0.022
- [ mitch] elapsed time (deleting 4 items): 0.008
- [ table.remove] elapsed time (deleting 4 items): 0.028
- [ mitch] elapsed time (deleting 5 items): 0.008
- [ table.remove] elapsed time (deleting 5 items): 0.031
- [ mitch] elapsed time (deleting 6 items): 0.008
- [ table.remove] elapsed time (deleting 6 items): 0.039
- Lua 5.3.4 Copyright (C) 1994-2017 Lua.org, PUC-Rio
- + $ lua test.lua
- [ mitch] elapsed time (deleting 1 items): 0.306
- [ table.remove] elapsed time (deleting 1 items): 0.121
- [ mitch] elapsed time (deleting 2 items): 0.309
- [ table.remove] elapsed time (deleting 2 items): 0.164
- [ mitch] elapsed time (deleting 3 items): 0.313
- [ table.remove] elapsed time (deleting 3 items): 0.237
- [ mitch] elapsed time (deleting 4 items): 0.326
- [ table.remove] elapsed time (deleting 4 items): 0.262
- [ mitch] elapsed time (deleting 5 items): 0.326
- [ table.remove] elapsed time (deleting 5 items): 0.304
- [ mitch] elapsed time (deleting 6 items): 0.334
- [ table.remove] elapsed time (deleting 6 items): 0.341
- ]]--
Add Comment
Please, Sign In to add comment