lua-users home
lua-l archive

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


Am 07.10.2013 00:59 schröbte Tim Hill:

On Oct 6, 2013, at 2:24 PM, Dirk Laurie <dirk.laurie@gmail.com> wrote:

2013/10/6 Tim Hill <drtimhill@gmail.com>:

I think there is some confusion of scope here. The implementation
of the # function is (not surprisingly) O(1) but the entire set of logic
to *realize* that behavior is O(n) since there is code for each
__newindex() call. That was the point of my original post, since
Roberto had raised concerns that my suggested design was O(n) ..
though, like Dirk's design, it was O(n) for __newindex() calls, and
was O(1) for the actual call to #.

My code is O(1) per __newindex call. Merely storing something in
a table is also O(1) per call, so O(1) per call is optimal.

There's a once-only startup pass over the whole table, which
amortizes to O(1) per table entry.

If you wish to track the largest index actually present in the table,
that would be O(n) per __newindex, unless you invest in maintaining
a priority queue.


… so it's O(1) per call, which is O(n) for n entries, which is O(n).

I think it is customary to specify big-O information for a single call of the operation you are interested in.

So, table insertion is O(1), sequence validation is O(n). I think the confusion started because Roberto tried to point out that constant overhead during table insertion ends up being of the same complexity as a sequence validation if you consider the combined operation, because you do n O(1) inserts and one[*] O(n) validation. And the performance of sequence validation was the subject we were talking about at that time ...

  [*]: or at least a fixed number not dependent on n


My original suggestion was O(n) in the sense of having a small fixed overhead per new item in the table, which would be O(1) in your terminology. I avoided the max issues by the slight change to the definition of sequence.

--Tim


Philipp