lua-users home
lua-l archive

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


2014-08-14 3:23 GMT+02:00 Jan Behrens <jbe-lua-l@public-software-group.org>:
>> So, by principle, this implementation should not seem inefficient.
>> (Never mind that quite probably this O(1) is slower than Lua's
>> built-in O(log n) length anyway :)
>>
>> -- Roberto
>>
>
> If Lua's built-in O(log n) length operator is really that fast, then
> caching the length might not make sense in my case.

In actual applications, finding #tbl is quite often followed shortly
by traversing tbl from 1 to #tbl soon afterwards. E.g.
   for i=1.#tbl do ...

I.e. the O(log n) length operator becomes insignificant in comparison
to the O(n) table traversal.