Optimized version of sumsquaredifference