In general, you shouldn’t worry about performance unless there is an actual performance problem. Even if one method is faster than another, the difference may not matter because those methods might not be the bottleneck. They could be waiting on other processes to complete, making their speed irrelevant. Additionally, the performance difference might be negligible compared to the impact of other parts of your code.
That being said:
Timers are likely the better choice, as coroutines can generate garbage, but this ultimately depends on the timer implementation.
The answer depends on several factors:
- What do you mean by “large numbers”?
- What other code is executed by the components using these timers?
- How do you define “minimal”?
- What is the target hardware?
- What type of game are you developing?
Also the performance hit may be due to being slower, or being faster but creating more garbage which means slower at certain points where the GC collects that garbage.
This is a very broad question, with different answers not only between timers and coroutines but also between different implementations of timers.
The only scenario where coroutines might be a better choice is if you’re short on time and unfamiliar with creating proper timers, so you opt to use what you already know. In most other cases, I believe timers are the better option, not just for performance reasons but also because they offer greater flexibility. Timers can be enhanced with features that are difficult or even impossible to implement with coroutines, such as pause/resume functionality, adding time to an active timer, or executing callbacks.
If you are interested check my post about timers: Various Timer Implementations in Unity and the github repository with the three mentioned timer implementations: https://github.com/meredoth/Unity-Timers
Finally, when it comes to performance-related questions, actual results are always more reliable than theoretical discussions. You can use Unity’s Performance Testing Package for Unity Test Framework to test the performance of different implementations. For isolated methods/code I think it serves better than the profiler.
EDIT: Also don’t use timers by adding/subtracting Time.deltaTime
, because the float precision errors accumulate over time. Just check the time now compared to the time you started the timer.