[gdal-dev] create 25D MultiPolygon how to

Mateusz Loskot mateusz at loskot.net
Sat Mar 28 14:09:52 EDT 2009


Joaquim Luis wrote:
> Mateusz Loskot wrote:
> 
>>>  for(Roadway::RoadWayArray::iterator itrw = _roadwayArr.begin(); itrw !=
>>> _roadwayArr.end();itrw++)
>>
>> ++itrw;
>>
>> if you care about performance.
> 
> Mateusz,
> 
> Just curious. Why should that impact on performance?

The itrw here is most likely an iterator of type of user's class.
The pointer arithmetic does work here only in terms of
semantic, but not in terms of implementation.
Meaning, both versions do the same - advance to next position:

int* p = ...; // or any ordinary type
p++;

iterator it = ...;
it++;

However, the realization is completely different.
Here is example of how pre- and post-increment operator is usually
declared in a class:

struct T
{
   T& operator++(); // pre-
   T operator++(int); // post-
};

The pre-increment operator returns reference to the object
itself (return *this;)

The post-increment returns a temporary copy of the object.
There are important side-effects of returning copy:
1. it is constructed -> copy-construction -> constructor called
2. it is returned by value -> copy-construction -> constructor called

Here is example of iterator and operator++ implementation:
http://liblas.org/browser/trunk/include/liblas/iterator.hpp?rev813#L108

Object construction is considered in C++ as an expansive operation,
next to dynamic storage allocation.
If post-increment/decrement operator is used against pointers and
integers, temporary object is also created, but as they are native
types no constructor call is involved.

People report different measurements on Usenet groups. Numbers
vary but can easily hit 50 % of performance increase if you
stick to use of pre-increment/decrement operator for class types.

Best regards,
-- 
Mateusz Loskot, http://mateusz.loskot.net
Charter Member of OSGeo, http://osgeo.org


More information about the gdal-dev mailing list