Re: Performance Question ...
Hi,
Konrad M?hler schrieb:
I've a list of objects. I iterate the list and read the value of each
object for many operation like:
x = myList[i].value1 + myList[i].value2
etc.
My question:
Is it efficient to always use myList[i] or should I get the pointer to
the current element and use this instead like
currentElement->value1 ...
Is it low-performance to always let the list return the i's element?
what is the complexity of myList.operator[]?
The complexity of your loop will be one order higher (unless you have
something less efficient somewhere else in the loop).
The STL containers usually do not provide operator[] unless it has low
complexity, usually O(1). So if you do not have your own implementation
of operator[] it is likely that you end up with O(n), which is the
minimum of a loop over the entire container content. The performance
impact is minimal in most cases.
However, if your operator[] has O(log(n)) (like SGI's rope
implementation) or O(n) like a linked list, you will have O(n*log(n)) or
O(n?) respectively, which is bad.
Marcel