xtensor
xtensor copied to clipboard
How to avoid memory allocation here?
I have procedure like below, what is the best and most performance practice for below procedure?
// what is the type here ?
auto tmp = xt::zeros<float>(shape);
if (condition1) {
tmp += v1;
}
if (contidion2) {
tmp += v2
}
return tmp;
The code won't compile, I'm here to ask for help how to code correct .
xt::zeros does not hold any value, as explained here. Therefore, if you want to use it in computed-assignment, you need to assign it to an xarray first, you cannot avoid the memory allocation.
So just to be clear, you will want to do either of the following:
auto tmp = xt::eval(xt::zeros<float>(shape));
xt::xtensor<float, n> tmp = xt::zeros<float>(shape); // if you know the rank at compile time
xt::xarray<float> tmp = xt::zeros<float>(shape); // if you don't (easily) know the rank at compile time