7 Replies - 228 Views - Last Post: 25 July 2012 - 12:38 PM Rate Topic: -----

#1 Zel2008  Icon User is offline

  • D.I.C Addict

Reputation: 14
  • View blog
  • Posts: 728
  • Joined: 06-January 09

Populating a very large vector

Posted 25 July 2012 - 11:17 AM

Hi all,

I have a vector of objects that I have to populate, and each object in the vector takes a certain amount of time to be built. The amount isn't significant for less than 100 objects, but after a while, it starts to take a very long time to populate the entire vector. I need to be able to build the vector fast for 1000 or more objects.

Does anyone know of a standard C++ way I could speed things up, rather than just using a simple for loop like this?
	vector<MyObject> myVector;
	for( int i = 1; i <= numObjects; i++ ) {
		MyObject* object = new MyObject();
		object->build( i );
		myVector.push_back( *object );
	}



I haven't been able to find a standard multithreading solution, and I'm not quite sure what to make of POSIX; is that reliable across systems?

I'd appreciate any insight or advice on this.

Thanks,
Zel2008

Is This A Good Question/Topic? 0
  • +

Replies To: Populating a very large vector

#2 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 1937
  • View blog
  • Posts: 5,769
  • Joined: 05-May 12

Re: Populating a very large vector

Posted 25 July 2012 - 11:22 AM

Have you considered lazy initialization? Do the MyObject instances really have to be in a fully initialized state when they are put into the vector? Or can you have the object initialize itself fully the first time you do a meaningful operation on it?
Was This Post Helpful? 0
  • +
  • -

#3 Zel2008  Icon User is offline

  • D.I.C Addict

Reputation: 14
  • View blog
  • Posts: 728
  • Joined: 06-January 09

Re: Populating a very large vector

Posted 25 July 2012 - 11:26 AM

Thanks Skydiver,
What would that look like, I haven't heard that term before?
Thanks,
Zel2008
Was This Post Helpful? 0
  • +
  • -

#4 jimblumberg  Icon User is offline

  • member icon

Reputation: 3058
  • View blog
  • Posts: 9,305
  • Joined: 25-December 09

Re: Populating a very large vector

Posted 25 July 2012 - 11:36 AM

First I really don't see the need for the dynamic memory. You aren't storing pointers in your vector so just create an instance of your object.

for( int i = 0; i < numObjects; i++ ) {
	MyObject object;
	object.build( i );
	myVector.push_back( object );
}


Since you know the size of the vector you should also consider creating the vector with the optional size parameter.
vector<MyObject> myVector(numObjects);

Then instead of using push_back just use the array index.
for( int i = 0; i < numObjects; i++ ) {
	myVector[i].build( i );

}

Also note: You should get used to the idea that arrays, vectors are zero based, meaning that the first element is at myVector[0] not 1 and they stop at element size-1.

Jim
Was This Post Helpful? 3
  • +
  • -

#5 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1690
  • View blog
  • Posts: 2,553
  • Joined: 21-June 11

Re: Populating a very large vector

Posted 25 July 2012 - 11:38 AM

Are you aware that your code is (heavily) leaking memory? Also since the vector doesn't store pointers, why are you allocating the objects using new at all just to create a copy of them?

Are you sure it's really the object creation time that's the primary problem and not the time it takes to allocate the memory on the heap and the time it takes to resize the vector? Because you can get rid of both of those. Also the time it takes to copy the items into the vector after you created them on the heap using new.
Was This Post Helpful? 1
  • +
  • -

#6 Zel2008  Icon User is offline

  • D.I.C Addict

Reputation: 14
  • View blog
  • Posts: 728
  • Joined: 06-January 09

Re: Populating a very large vector

Posted 25 July 2012 - 11:48 AM

Thank you both! Wow, I didn't realize I was leaking so much memory! I'll clean that up and see if it works faster -- you might be right about that being the culprit.

EDIT: Well, it doesn't look like that was it, but I'll try that lazy initialization thing, maybe that will help.

This post has been edited by Zel2008: 25 July 2012 - 12:03 PM

Was This Post Helpful? 0
  • +
  • -

#7 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 1937
  • View blog
  • Posts: 5,769
  • Joined: 05-May 12

Re: Populating a very large vector

Posted 25 July 2012 - 11:49 AM

Assuming your MyObject looks like:
class MyObject
{
public:
    MyObject()
    {
        DoBasicInitialization();     // may take some time
    }

    void build(int buildParameter)
    {
        DoFinalInitialization(buildParameter);    // may take some time
    }

    void SomePublicMethod()
    {
        // Do work
    }

    void SomeOtherPublicMethod()
    {
        // Do work
    }
};



You change it over to:

class MyObject
{
public:
    MyObject()
        : _isInited(false), m_buildParam(-1)
    {
    }

    void build(int buildParameter)
    {
        _buildParameter = buildParameter;
    }

    void SomePublicMethod()
    {
        EnsureInitialized();
        // Do work
    }

    void SomeOtherPublicMethod()
    {
        EnsureInitialized();
        // Do work
    }

private:
    bool _isInited;
    int _buildParameter;

    inline void EnsureInitialized()
    {
        if (!_isInited)
        {
            DoBasicInitialization();
            DoFinalInitialization(_buildParameter);
            _isInited = true;
        }
    }
};



You can also probably come up with a Lazy<> template class that can contain any object and only instantiate it at the last possible moment. It's better suited for objects that are expensive to create. It'll also require a bit more planning for objects that have 2 stage initialization like your class. On the plus side, you'll be able to recycle the Lazy<> template class for other uses.
Was This Post Helpful? 1
  • +
  • -

#8 Zel2008  Icon User is offline

  • D.I.C Addict

Reputation: 14
  • View blog
  • Posts: 728
  • Joined: 06-January 09

Re: Populating a very large vector

Posted 25 July 2012 - 12:38 PM

Thank you everybody, I got everything working. I learned a lot today, thank you so much for your clear explanations and patience! :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1