memory - Small C++ container -


short story: need container can store unlimited number of elements, no dynamic memory allocation small number of elements.

long story: need store couple of integers, number of not bounded, more 2 or three. tried using vector<int> or list<int>, performance compared simple array worse factor of @ least 50 in release mode, , 500 in debug mode. example, measured creating vector, calling push_back 3 times, , destroying again. takes around 250ns. calling reserve(3) upfront helps bit, time goes down 100ns. using array instead takes 3ns, cannot use fixed-size array because number of elements unbounded. need store lot of things, part of larger objects.

so guess i'm asking container uses sort of small vector optimization: long number of elements small stored within object itself, , when grows beyond limit allocates memory on heap. such thing exist?

as side node, basic_string known have sort of optimization, tried basic_string<int>. indeed, size of 3 it's 4 times faster vector<int>, somehow using string class storing integers doesn't feel right. again, question: there container performs small size, if needed can grow unlimited?

edit people asked actual code. well, there nothing special it, it's straighforward. cannot post because it's part of large benchmarking environment, special purpose classes timing, , classes obfuscating code prevent compiler replacing {}. know, when notices result isn't used. core of comes down this:

vector<int> v; v.push_back(17); v.push_back(19); v.push_back(23); 

and

int x[3]; int k = 0; x[k++] = 17; x[k++] = 19; x[k++] = 23; 

each of run in tight timing loop.

edit2 use case tree variable number of branches @ each node. need store pointers , no integers, on 32-bit both same size. performance of important, because tree modified.

edit3 people keep doubting numbers. why think unreasonable? vector dynamic memory allocation, array doesn't, , seems explanation difference, or isn't it?


Comments

Popular posts from this blog

java - WrongTypeOfReturnValue exception thrown when unit testing using mockito -

php - Magento - Deleted Base url key -

android - How to disable Button if EditText is empty ? -