CPU Switching from one mode to another takes time. Therefore is is better programming practice to group similar kind of instructions together.
Ex: The Pentium MMX processor provided enhanced instruction set for multimedia support. However the processor had to be switched into different mode before the mmx instruction could be executed.
This code is an un-optimized code since in every alternate instruction CPU switches mode and thus consumes time uselessly. A smart compiler may optimize it by itself. However, relying on the compiler is not always a good option. (As all of us are still waiting for an AI enabled compiler)
I/O is done by OS, System calls involve CPU mode switching. So same holds true for I/O, DMA ( Direct Memory Access) and PIC ( Programmable Interrupt Controller )calls also.