In the context of driven diffusive systems, for thermodynamic transformations over a large but finite time window, we derive an expansion of the energy balance. In particular, we characterize the transformations which minimize the energy dissipation and describe the optimal correction to the quasi-static limit. Surprisingly, in the case of transformations between homogeneous equilibrium states of an ideal gas, the optimal transformation is a sequence of inhomogeneous equilibrium states.