Optimal way of making timed code

There are some methods for this on good controllers: for example, on STM32 you can control PWM output of the slave timer with another (master) timer, so you can switching output on and off with precise intervals.
On different controllers PWM allowed you to generate a PWM pulse train with defined # of pulses (WinnerMicro W80x serie).