0

i always reading new is slow while allocating some memory, but never find out how slow it is. so i started some research and tests.

assume, i have a buffer which is a vector (e.g. for an ethernet receiver).

  1. i can make this buffer a member of a class, fill it with data and after copy this buffer to a userdefined buffer.
  2. i can create a new buffer and move it to the userdefined.

so now my question is which was faster. while searching the net i didn't realy find any benchmark or something else. so i started some test's.

the 2 varints are not receivers!

copy variant

auto time = GetTickCount();
std::vector<int> vec;
std::vector<int> tmp(250);

for(int i=0; i<10000; i++) {
  for(int i=0; i<1000; i++) {
    vec.insert(vec.end(), tmp.begin(), tmp.end());
    //std::copy(tmp.begin(), tmp.end(), std::back_inserter(vec1));
  }
  vec.clear();
}

std::cout << GetTickCount() - time << std::endl;

move variant

auto time = GetTickCount();
std::vector<std::vector<int> > vec;

for(int i=0; i<10000; i++) {
  for(int i=0; i<1000; i++) {
    std::vector<int> tmp(250);
    vec.push_back(std::move(tmp));
  }
  vec.clear();
}

std::cout << GetTickCount() - time << std::endl;

i know allocating memory is depending on the hardware, os and memory managment, but is there a average time i could say it is better to create a new one and move it than copieng the existing one? in my tests, i found coping a vector with 250 elements needs about the same time as moving and more than 250 elements the copy variant is slower than the moving one. sure in my test the moving variant is a vector of vectors and iterating is more difficult, but this doesn't matter in (most of) my case(es). also the test is with int's and not some struct's or classes which would complicate the question.

also my test is (fast and dirty) on a windows machine. my interest is an average time (or in my condition, size) for ussual hardware and ussual systems (windows, linux, mac).

could i take my test and say copying more than 400 elemtents is slower than creating a new one?


edit:

Neil Kirk adviced me to try random size allocation, so i did. also i inserted some other stuff doing allocations (but no deletes) between the test's and the average size incressed to about 1000 elements.

i accept Mats Petersson's answer, especially after reading this (and the sublinks) and this (beside it's the only one). but i have 1 addition: you can't know if it is premature optimisation without any valuation. if an alloc would take the same time as copying 100000 elements i never would use it, and otherwise if it would take the same time as copy 10 elements i would use it alway's. but with the approximated value of 1000 elements in a network scenario where i have the network as bottleneck i could say it is premature optimisation. so i decide to use ne allocating variant, because it's more usefull in my concept.

Community
  • 1
  • 1
user1810087
  • 5,146
  • 1
  • 41
  • 76
  • @R.MartinhoFernandes i thought c++11 would imply c++ :) – user1810087 Sep 16 '13 at 14:36
  • Yes, but the website doesn't know that :) (i.e. it influences filters and searches) – R. Martinho Fernandes Sep 16 '13 at 14:40
  • It is never too late to learn... thx – user1810087 Sep 16 '13 at 14:42
  • Try making allocations of random size, to make it harder on the system to find free blocks. Also try sampling the time after you've left the test running a while, for memory fragmentation. – Neil Kirk Sep 16 '13 at 14:43
  • Also bear in mind that this will be highly dependent on the implementation of the standard library you are using. C++ does not define a memory model that must be used for it's implementation, so each implementation uses a slightly different model. Some are more efficient than others at certain tasks. – Zac Howland Sep 16 '13 at 14:47
  • Also `GetTickCount()` is only accurate to 10-16 milliseconds according to the documentation. – graham.reeds Sep 16 '13 at 14:54
  • @graham.reeds this doesn't really matter, my test is running about 20 seconds each variant. – user1810087 Sep 16 '13 at 14:59

1 Answers1

2

This is a typical case of "premature optimisation". Find out if this part of your code is an important factor with regards to performance. If it's not, then don't worry about it - do whatever makes the most sense from the actual task in hand.

In general, allocating memory is reasonably fast - for anything other than basic types (int, char and such), the main factor is probably the time it takes to create/copy/move the object that goes into the vector, rather than the basic allocation.

Mats Petersson
  • 126,704
  • 14
  • 140
  • 227