Alternative to computation in a loop for distances for fast performance