Re: C++11 moves

Posted 02 September 2013 - 10:49 AM

Quote

I see why is would move after the create, but why not move the existing objects too?

I'm not sure why the copy constructor is being evoked instead of the move copy constructor, but the problem is being caused by the reallocation of the vector. For this small sample you can probably increase the speed by altering the amount of memory reserved for the vector. With my compiler the capacity of the vector increases like:

1
2
4
8
16
32

I find the fastest way to increase the speed of vector operations is to try to avoid as many reallocations as possible.

If you reserve a larger amount then your reallocation will happen much less often.

Re: C++11 moves

Posted 02 September 2013 - 11:00 AM

Yes, I read somewhere that every time the vector needs to reallocate it doubles the size of the allocation in order to limit the average number of copies to 2 per item. I was thinking I might estimate the maximum size of a population over the duration of a run and start with that. Still, it would be nice to get move working throughout. It may be that the STL hasn't fully updated the vector class yet.

Re: C++11 moves

Posted 03 September 2013 - 09:07 AM

Quote

I will need to profile the code, but I'd like to know what is generally faster. I assumed that move would be, but it doesn't seem to be doing a byte by byte copy like I had originally thought, so maybe not.

The answer is, it depends. The purpose of move is, not to do byte by byte copies, but to avoid unecessary memory allocations and copies from temporaries. If your objects does not use any dynamic memory, or STL containers, then a move constructor and copy constructor will usually have the same performance. Move constructors are used to steal pointers away from temporaries.