optimization of speed in _get_dist