Speed up computation of emperical distribution in gradient