lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


On Mon, Nov 25, 2013 at 7:03 PM, Sean Conner <sean@conman.org> wrote:
> It was thus said that the Great John Hind once stated:
>> > Date: Sun, 24 Nov 2013 17:58:12 -0500
>> > From: Sean Conner <sean@conman.org>
>> > Subject: Re: table library changes (was Re: table.new in 5.3?)
>> >
>> >
>> >   So, aside from a performance optimization, yes, it can all be done in
>> > pure
>> > Lua.  But I also don't follow your "encourages inefficient coding'
>> > logic
>> > here.
>>
>> Having the existing single element table.remove and table.insert encourages users to write code like:
>>
>> t = {}
>> for i=1, 10000 do t[i] = i end
>> for i=10, 100 do table.remove(t, i) end
>>
>> which is about 1000,000 table writes.
>>
>> An optimised algorithm looks something like this:
>>
>> t = {}
>> for i=1, 10000 do t[i] = i end
>> for i=11, 9899 do t[i] = t[91+i] end
>> for i=10000, 9900, -1 do t[i] = nil end
>>
>> only about 10,000 table writes.
>>
>> Clearly, the optimised algorithm could be written in C as a multi-value
>> remove library function, and like any algorithm it will be a faster in C
>> than in Lua. But I do not think it will be sufficiently faster to justify
>> its place in a C library.
>
>   I actually went to the trouble to measure the code and indeed, the
> "optimized" version does indeed run faster (about 10x), *BUT* it does *NOT*
> produce identical results.
>
>   I realize you were trying to prove a point here, but it might be made a
> bit better if both code fragments actually did identical work.
>
>   -spc
>

What's the difference? And would it help to make the final loop
"table.remove(t, i)" instead of "t[i] = nil"?

/s/ Adam