CxxUtils - Add DynamicArray container
Hi everybody,
in !47670 (closed), we needed a container with dynamic memory allocation and that supports the multi-dimensional C-array syntax in order to keep the number of changes small. One solution might be to use the ugly construct of
std::vector<std::vector<std::vector<int>>> my_vec;
Therefore I decided to write the DynamicArray
container which is based on a std::vector
and supports the required syntax features.
CxxUtils::DynamicArray<unsigned int, 3> myArray(3,5,6);
for (unsigned int i =0 ; i < 3; ++i){
for (unsigned int j=0; j < 5; ++j){
for (unsigned int k =0; k < 6; ++k){
myArray[i][j][k] = 5;
}
}
}
Given that the class is quite generic and therefore has potential more usecases than BIS78 cabling - at least, I spontaneously know dozens of places in the muon code - I thought it might be useful to put this somewhere central such that others can also benefit from it. So I cleaned-up the code added some more documentation & wrote a unit test.
The unit test also contains a benchmark part. This is deactivated by dummy compiler flags as it relies on TStopWatch
& TRandom
and ROOT is not currently a requirement of CxxUtils
.
I benchmarked the code with the following parameters
constexpr size_t dim1 = 200;
constexpr size_t dim2 = 300;
constexpr size_t dim3 = 100;
constexpr size_t dim4 = 50;
constexpr size_t total_dim = dim1*dim2*dim3*dim4;
constexpr int random_access = 100000000;
and also instantiated a classical C array as a performance reference.
Allocation of 300000000 elements took 0.34 seconds.
Assignment to -999 took 0.15 seconds.
Fine that basically tests the power of the std::vector
Filling test with random values
Element by element assignment using [][][][] operators took 161.5 seconds.
vs.
Element by element assignment for the classical array took 161.1 seconds.
Random read access
Random memory access using [][][][] took 55.55 seconds
Random memory access using get(i,j,k,l) took 55.03 seconds
vs.
Random memory access for the classical array took 55.34 seconds
Swapping of two random elements
Random swapping of 2 elements using [][][][] took 68.17 seconds.
Random swapping of 2 elements using get(i,j,k,l) took 67.82 seconds.
vs.
Random swapping of 2 elements for the classical array took 71.78 seconds.
Overall the performance is quite comparable with the classical C-array
Let me tag: @ssnyder, @christos, @sroe, @akraszna in cases they've comments/objections in adding this class here.
Tagging: @goblirsc, @pgadow, @sabidi, @sangelid, @npetters, @atlasbot for another episode of the philosopher of the day