Optimized difference calculation