C++ Vector with the possibility of storage in data in the memory of the object

Hello. This small article is dedicated to the StaticVector class, with similar std::vector

The main distinguishing feature of this class is that it stores can store data in the inner fields of the object, if the volume of data does not fit in the inne…


This content originally appeared on DEV Community and was authored by VladimirM

Hello. This small article is dedicated to the StaticVector class, with similar std::vector

The main distinguishing feature of this class is that it stores can store data in the inner fields of the object, if the volume of data does not fit in the inner fields of the class, then the memory is allocated in a heap. This approach allows you to reduce the execution time on the allocated of memory.

If the memory is allocated in the heap of the size of the block of the allocated block is determined by the following rules:

This blog is conducted with two purposes so that the written things were beneficial and was not boring. Therefore, I also apply the video how the development of this class took place.

Description of the degree function

That is, in the case of memory of memory in a heap, we distinguish memory with a reserve. The size of the buffer in this case is determined by the degree function of the degree N.

This power function can be set as a linear increase

OffsetSize = 0;
StepSize = 16;
StepModule = 1;

N   BufferSize
0   8
1   16
2   24
3   32

and increase in power mode

OffsetSize = 0;
StepSize = 2;
StepModule = 2

N   BufferSize
0   2
1   6
2   14
3   30

Using this function, you can get the result equivalent to 2^N by setting the OffsetSize parameter, equal to 2.

OffsetSize = 2;
StepSize = 2;
StepModule = 2

N   BufferSize
0   4
1   8
2   16
3   32

Description of a fixed increase function

For superly large values, the size of the buffer can be calculated according to the second function, which has a fixed increase in the size of the buffer when calculating the next step and is used if the resulting sizes of the buffer exceeds the value of MaxPow

MaxPowStepSize step size is determined as the difference in the steppe function of the following after the MaxPow and its previous value.

F(Laststep) - F(Laststep -1)

And MaxPowStart is the first value of the degree function that follows the MaxPow parameter.

A brief description of the class

The class template has the following view:

template <
  typename Ty, 
  unsigned int StaticSize=16, 
  unsigned int OffsetSize=2, 
  unsigned int StepSize=2, 
  unsigned int StepModuleInt=2, 
  unsigned int StepModuleFrac=0, 
  unsigned int MaxPow=0>
class StaticVector;

Description of the parameters of the template

typename Ty - Type of stored elements
unsigned int StaticSize - Maximum number of elements stored in the internal buffer
unsigned int OffsetSize - The offset size used in the calculation function
unsigned int StepSize - The step size used in the calculation function
unsigned int StepModuleInt - The integer part of the StepModule parameter used in the calculation function
unsigned int StepModuleFrac - Fractional part of the StepModule parameter number used in the calculation function
unsigned int MaxPow - The limit value after which the buffer size calculation goes into linear form and a simple increment is performed. If the value is 0, but this functionality is not used

The class itself in use is similar to std::vector. It has iterators, access to the index element and the push_back() and pop_back() operation

fcf::StaticVector<int, 32> v; 
for(int j = 0; j < a_vecSize; ++j) {
  v.push_back(j); 
} 

for(auto it = v.begin(); it = v.end(); ++it) { 
  std::cout << *it << std::endl;
} 

The speed of execution

And most importantly. Comparison with the speed of std::vector.

Comparison of push_back operations and creating a container with a given value.

Launching under Linux (GCC)

Speed compare StaticVector with std::vector...

Number of iterations: 1024
StaticSize parameter: 64

                               StaticVector std::vector  std::vector/StaticVector

Push back test (1 items):      79           277          3.50633
Push back test (4 items):      98           730          7.44898
Push back test (8 items):      136          1044         7.67647
Push back test (16 items):     216          1413         6.54167
Push back test (64 items):     764          2824         3.69634
Push back test (256 items):    4824         7066         1.46476
Push back test (1024 items):   16999        22544        1.3262
Push back test (32768 items):  500962       549807       1.0975
Push back test (131072 items): 2072654      2274810      1.09753
Initialize test(1 items):      84           165          1.96429
Initialize test(4 items):      97           175          1.80412
Initialize test(8 items):      100          195          1.95
Initialize test(16 items):     169          263          1.55621
Initialize test(64 items):     493          492          0.997972
Initialize test(256 items):    1640         1487         0.906707
Initialize test(1024 items):   5733         5524         0.963544
Initialize test(32768 items):  164230       172003       1.04733
Initialize test(131072 items): 657719       688127       1.04623

Launching Windows (Visual Studio 2022)

Speed compare StaticVector with std::vector...

Number of iterations: 8192
StaticSize parameter: 64

                               StaticVector std::vector std::vector/StaticVector

Push back test (1 items):      18           648         36
Push back test (4 items):      56           2546        45.4643
Push back test (8 items):      99           4023        40.6364
Push back test (16 items):     169          5447        32.2308
Push back test (64 items):     990          10954       11.0646
Push back test (256 items):    6589         13558       2.05767
Push back test (1024 items):   22087        24319       1.10105
Push back test (32768 items):  753411       1091349     1.44854
Push back test (131072 items): 3126283      2996807     0.958585
Initialize test(1 items):      163          704         4.31902
Initialize test(4 items):      173          724         4.18497
Initialize test(8 items):      193          714         3.69948
Initialize test(16 items):     201          907         4.51244
Initialize test(64 items):     238          988         4.15126
Initialize test(256 items):    1268         1151        0.907729
Initialize test(1024 items):   2714         2874        1.05895
Initialize test(32768 items):  82990        80453       0.96943
Initialize test(131072 items): 388915       384096      0.987609

The first and second column is the time of the test, and the third is the ratio of the second column to the first

As can be seen from the results with a small volume of Staticvector has a significant increase in performance, but it all depends on the platform

You can download a separate class from the repository to github: https://github.com/fcf-framework/short-fcfcpp-StaticVector


This content originally appeared on DEV Community and was authored by VladimirM


Print Share Comment Cite Upload Translate Updates
APA

VladimirM | Sciencx (2025-07-13T01:23:33+00:00) C++ Vector with the possibility of storage in data in the memory of the object. Retrieved from https://www.scien.cx/2025/07/13/c-vector-with-the-possibility-of-storage-in-data-in-the-memory-of-the-object/

MLA
" » C++ Vector with the possibility of storage in data in the memory of the object." VladimirM | Sciencx - Sunday July 13, 2025, https://www.scien.cx/2025/07/13/c-vector-with-the-possibility-of-storage-in-data-in-the-memory-of-the-object/
HARVARD
VladimirM | Sciencx Sunday July 13, 2025 » C++ Vector with the possibility of storage in data in the memory of the object., viewed ,<https://www.scien.cx/2025/07/13/c-vector-with-the-possibility-of-storage-in-data-in-the-memory-of-the-object/>
VANCOUVER
VladimirM | Sciencx - » C++ Vector with the possibility of storage in data in the memory of the object. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2025/07/13/c-vector-with-the-possibility-of-storage-in-data-in-the-memory-of-the-object/
CHICAGO
" » C++ Vector with the possibility of storage in data in the memory of the object." VladimirM | Sciencx - Accessed . https://www.scien.cx/2025/07/13/c-vector-with-the-possibility-of-storage-in-data-in-the-memory-of-the-object/
IEEE
" » C++ Vector with the possibility of storage in data in the memory of the object." VladimirM | Sciencx [Online]. Available: https://www.scien.cx/2025/07/13/c-vector-with-the-possibility-of-storage-in-data-in-the-memory-of-the-object/. [Accessed: ]
rf:citation
» C++ Vector with the possibility of storage in data in the memory of the object | VladimirM | Sciencx | https://www.scien.cx/2025/07/13/c-vector-with-the-possibility-of-storage-in-data-in-the-memory-of-the-object/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.