Quick little thing related to the previous post.
Unless otherwise disabled, the ZX Spectrum hardware issues an interrupt at the start of the processing of each TV frame (actually the “retrace” if I recall). This means to get “in sync” with the screen simply issue a “HALT” Z80 instruction. After the interrupt the code will resume.
For poor-man timing any 50 FPS routine, play with the border colour, for example, “LD A,2” then “OUT (254),A” at the start to turn the border red, and “LD A,0” and “OUT (254),A” at the end to turn the border black. This was a favourite trick of mine, used when working on the previously mentioned “huge” text routine.