  We generalize domain-wall dynamics to the case of translationally
noninvariant ferromagnetic nanowires. The obtained equations of motion make the
description of the domain-wall propagation more realistic by accounting for the
variations along the wire, such as disorder or change in the wire shape. We
show that the effective equations of motion are very general and do not depend
on the model details. As an example of their use, we consider an
hourglass-shaped nanostrip in detail. A transverse domain wall is trapped in
the middle and has two stable magnetization directions. We study the switching
between the two directions by short current pulses. We obtain the exact time
dependence of the current pulses required to switch the magnetization with the
minimal Ohmic losses per switching. Furthermore, we find how the Ohmic losses
per switching depend on the switching time for the optimal current pulse. As a
result, we show that as a magnetic memory this nanodevice can be $10^5$ times
more energy efficient than the best modern devices.
