Optimize speed calculation