I hope you like a non-answer.
There rarely ever is a theoretically optimal time to tack that can be described in the abstract, as there are so many unknown, random variables. The best time to tack, for pure speed, is never. But never isn't a practical answer, as you usually have an ultimate destination or things to avoid, and the wind isn't constant, so you have to weigh your current course against those things, and so it usually boils down to experienced judgement and gut feeling.
Let's look at two simple beating strategies:
Which is objectively better? Neither. Both strategies are appropriate for different situations.
Lets imagine boat B is chasing boat A. Boat B may be able to overtake boat A by not wasting as much speed tacking so often. But what happens if boat A decides to change course while B is tacking so far away? What happens if the wind changes unfavorably while B is so far away? You have to make choices while predicting the future, a very challenging task.
Here is another example of a course decision:
This is an illustration of a racing situation. In here, we are rounding the triangular mark counter-clockwise to the left. The solid lines and the dashed lines are two possible courses. If you look at where the two lines split, between there and the place where the dashed line turns, is a sort of 'no-go' zone for the dashed line course. If you turn later than the solid line does, you'll have to turn later again to avoid hitting the mark. But if you wait not long enough, you'll have to do two short, back to back tacks, which can be brutal on your speed and kills your maneuverability. If you take the dashed course, you can see there is somewhat an optimal place to turn, right where you are now able to reach across the wind at maximum speed and pass just alongside the mark. Going any further and you're wasting time.
But which of those two courses is better? No way to say. The wind could shift towards the left while C is just below the mark and really ruin its skipper's day, or it could shift towards the right and do the same to D.
Different wind directions, presence of other boats, presence of other hazards, all makes deciding when and where to turn challenging for a skipper, especially in a wartime or racing situation.
With some time at the helm sailing, one can develop an intuitive feel for how best to pilot a course, but people have written vast books to try to cover the entire subject and you could read them all and would still need practice.
In theory, there is no difference between theory and practice. But, in practice, there is.