lua-users home
lua-l archive

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


On Aug 13, 2014, at 11:16 PM, Dirk Laurie <dirk.laurie@gmail.com> wrote:

> 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.
> 

Quite apart from the fact that on many processors the # operation probably warms the CPU caches ready for the traversal.