Split up loop to speed up compute_sum.