Speed up some operations on dense tensor a lot by removing method call in inner loop