In some circumstances, my C++14 program needs a "block" of about 100 millions complex<float>
, which requires close to 1 GB of RAM. We can safely assume that the required memory will be available.
However allocating a new std::vector
is really slow, because the complex constructor is called 100 millions time. On my machine, the code requires around a full second to initialise the array.
By comparison, calling calloc()
, which initialises the allocated memory to zero, with mostly the same effect, will run in a very small number of milliseconds.
It turns out we don't even need this initialisation, because the complex in the large array will be populated shortly later from an external source. I am looking therefore at deferring the construction of the objects in that "block" to a later time, and then construct them directly from the external data source.
So my question is, is there a safe idiomatic and efficient C++ way to do that, perhaps using C++ move semantics along the way? If not, and we decide to simply malloc
the memory block, can we then simply reinterpret_cast
the memory block to a plain old C array of complex<float>
?
Thank you for helping
Jean-Denis Muys