[Insight-users] Using c++ stl containers in the transform?

motes motes mort.motes at gmail.com
Fri Dec 11 12:08:16 EST 2009


I use std::map<int, std::vector<MyObj>> myMap;

In the transform I do:


template<class TScalarType, unsigned int NDimensions, unsigned int VSplineOrder>
void
MyBSplineDeformableTransform<TScalarType, NDimensions, VSplineOrder>
::TransformPoint(
  const InputPointType & point,
  OutputPointType & outputPoint,
  WeightsType & weights,
  ParameterIndexArrayType & indices,
  bool & inside ) const
{

int v = hash(point); // Simple add/subtract function to map a point
into the std::map predefined keys.
std::vector<MyObj> & myObjs = myMap[key];
std::vector<MyObj>::iterator it = myObjs.begin();
while (it!= myObjs.end()) {

  // do stuff
  it++;

}

The number of elements in myObjs range from 10-40. When I do nothing
in the body of the while loop it still adds a large overhead of time
consumption (compared to the original transform).

When I remove the while loop it runs as fast as the original transform.




On Fri, Dec 11, 2009 at 4:28 PM, Luis Ibanez <luis.ibanez at kitware.com> wrote:
> Hi Motes,
>
> Please see comments interleaved with your text below.
>
>    Thanks
>
>             Luis
>
>
> -------------------------------------------
> On Thu, Dec 10, 2009 at 12:09 PM, motes motes <mort.motes at gmail.com> wrote:
>> On Thu, Dec 10, 2009 at 3:23 PM, Luis Ibanez <luis.ibanez at kitware.com> wrote:
>>> Hi Motes,
>>>
>>> Before we dive into the details of your code....
>>>
>>> Are you compiling you application for "Release"  ?
>>
>>
>> Yes
>>
>>> In order to do profiling comparisons, you must
>>> start first by making sure that you are using the
>>> compiler's optimization.
>>>
>>>
>>> Using STL containers is as efficient as using
>>> pointers, as long as you access the elements
>>> of the container via STL iterators.
>>
>> I have not found any documentation that says that doing:
>>
>> std::vector<std::string> container;
>> // Add 100000 strings to container
>>
>> std::vector<std::string>::iterator it = container.begin()
>> while (it != container.end()) {
>>  std::string str = *it;
>>  // do something with str
>>  it++;
>> }
>>
>> is faster than:
>>
>> int size =  container.size();
>> for (int i=0; i< size; i++) {
>>  std::string str = container[i];
>>  // do something with str
>> }
>>
>>
>> I have tried both implementation in my transform and I do not
>> experince any difference in performance.
>>
>
>
>
> Yes, that's exactly what I said in my previous email:
>
> That using STL iteratos is *as fast as* using pointers.
> (as long as you compile for Release).
>
>
>
>>
>>> Do the operations of mapping points involve
>>> inserting new elements or removing exising
>>> elements from the STL containers in your
>>> Transform  ?
>>>
>>
>> I only lookup existing elements and read the contents of those. I do
>> not add or delete any elements.
>>
>>
>
>
> What method are you using for "looking up" elements in the container ?
> and ... what container are you using ? (std::vector ? std::map?).
>
>
>>
>>
>>
>>>
>>>    Please let us know,
>>>
>>>
>>>        Thanks
>>>
>>>
>>>             Luis
>>>
>>>
>>> --------------------------------------------------
>>> On Thu, Dec 10, 2009 at 7:09 AM, motes motes <mort.motes at gmail.com> wrote:
>>>> I have created a transform (TransformPoint) that iterates a
>>>> std::vector base on a lookup in a std::map. I can't get the same
>>>> performance compared to running the  BSplineDeformableTransform and I
>>>> suspect that its beause I use the stl containers. Typically I only
>>>> iterate 10-40 elements in the vector based on a single lookup in the
>>>> map (lg n time).
>>>>
>>>> Does anyone have experiences with the pros and cons of using the stl
>>>> containers in per pixel/voxel functions like the TransformPoint in the
>>>> BSplineDeformableTransform?
>>>> _____________________________________
>>>> Powered by www.kitware.com
>>>>
>>>> Visit other Kitware open-source projects at
>>>> http://www.kitware.com/opensource/opensource.html
>>>>
>>>> Kitware offers ITK Training Courses, for more information visit:
>>>> http://www.kitware.com/products/protraining.html
>>>>
>>>> Please keep messages on-topic and check the ITK FAQ at:
>>>> http://www.itk.org/Wiki/ITK_FAQ
>>>>
>>>> Follow this link to subscribe/unsubscribe:
>>>> http://www.itk.org/mailman/listinfo/insight-users
>>>>
>>>
>>
>


More information about the Insight-users mailing list