Re: Lists; a fast way to store objects.
Posted: Sat Dec 17, 2016 9:55 pm
Could I see an example of that slowing down? I only see one that appears linear-ish, by modifying main.lua so it holds ~5000 elements.
The slowdown happens when inserting elements. On my laptop, the insertion section of benchmark I posted earlier takes about 3.5 seconds for 10,000 elements. With 5000 elements, it takes about 0.7 seconds (under LuaJit, testing Tjakka's solution).pgimeno wrote:Could I see an example of that slowing down? I only see one that appears linear-ish, by modifying main.lua so it holds ~5000 elements.
Nice, it's much faster without those asserts. It's strange that LuaJit can't manage to do that as efficiently as PUC Lua.pgimeno wrote:Thanks. Found the cause.
It turns out to be the caveat I alerted about here: http://lua.space/general/assert-usage-caveat
Commenting out the assert at the top of add() solves the issue (for me).
Don't fall for the fallacy of premature optimization. It makes sense when it makes sense.WetDesertRock wrote:Were you finding slow downs with tables as arrays? Or is this a pre-optimization?
Code: Select all
t[new_item]=1 -- insertion
t[old_item]=nil -- deletion
if t[some_item] then ... -- checking for existence
for v in pairs(t) do ... -- parsing
Code: Select all
t[#t]=nil -- remove
t[#t + 1]=new_item -- add new
Code: Select all
t[i]=t[#t] -- replace by last element
t[#t]=nil -- remove last element
kikito wrote:Inserting things not-at-the-end is the only place where I would need table.insert. But in reality I never do that.
Not that this would be that common of a scenario either, but:kikito wrote:When I need to remove some non-last value, I can replace that value by the last one, and then remove the last one. Like this:Code: Select all
t[i]=t[#t] -- replace by last element t[#t]=nil -- remove last element
Code: Select all
local yesThisIsLuaCode = nil -- (ignore this)
t[#t] = t[i] -- move old to end
t[i] = e -- new element inserted wherever wanted
-- or in a more concise form:
t[#t], t[i] = t[i], e