Squeezing out cycles from loops

When studying Z80 programming, we usually learn that is better to use relative jumps (JR) in loops since they save us a byte in code compared to absolute jumps (JP).

Well, that’s certainly the case, but when you are squeezing out cycles from code, one place to look at is the loops you have.

A conditional relative jump wastes 12 cycles (t-states) when the condition is not met, and 7 cycles otherwise:

JR NZ, n -> 12T (unmet condition) / 7T (met condition)

Conversely, a conditional absolute jump always wastes 10 cycles, regardless the condition is met or not*:

JP NZ, nn -> 10T

Since a loop has n-1 iterations where the condition is not met, we can save 2 cycles per iteration just by changing a JR instruction for the corresponding JP one, paying just a one-byte penalty.

If your code has many loops with a big number of iterations each, changing the jump instructions may speed up a little your program.

* A conditional absolute jump always takes 10 T-states to execute because when the condition is not met, the Z80 still reads the next two bytes in the jump instruction in order to point the PC register to the next instruction.