I pre-allocate a large vector of N variables as a complex<double>
that function A acts on. Once it is done, I wish to pass it on to another function B, where I wish to re-use that memory allocation, but as a vector of length 2N of data type double
, i.e.:
vector<complex<double>> vec(N);
for (int i = 0; i < M; i++;
{
Function A(&vec); // Does work on vec as a vector<complex<double>>
// vector<complex<double>> vec(N) converts to vector<double> vec(2N) (or vector<float> vec(4N))
Function B(&vec); // Does work on vec as a vector<double> or vector<float>
}
where M
and N
are large enough to warrant pre-allocation of vec
. I am trying to avoid allocating as it is costly. The data is independent of each other (i.e. I will end up populating the data again before I use it in Function B). From what I have read so far, (anonymous?) unions seem the way to go, but I am unsure as to how unions work when it comes to vectors. I am hoping I could get some assistance in this matter.
Thank you.
Edit: It takes 3 seconds to allocate the vector, and the function takes 5 seconds to operate on.