I don’t think you will see much differences there regarding the performance.

Note: if you want to replace the 4s by arbitrary values defined in a tensor, I would recommend masked_scatter_ 531 instead of looping over tensors yourself. This approach should also work with a tensor of always the same value (e.g. always 16). However, this function will also modify the original tensor. If you don’t want this behavior, you would also need to clone it before.