I need to check first five letters of gameobject.name / tag to check if the string containing “Player” (Player1,Player2…)
Already figured out it can be done with substring or regex:
Is there any difference with performance?
I need to check first five letters of gameobject.name / tag to check if the string containing “Player” (Player1,Player2…)
Already figured out it can be done with substring or regex:
Is there any difference with performance?
There probably is a difference, but I would guess it’s so minimal that it wouldn’t matter in the end. The only way to find out is to make a test and benchmark it yourself. There’s script in the Unity community that can help (link here). If you are really concerned about performance, you should be avoiding string comparisons and tags all together.
Although using strings and tags, usually keeps things readable and easy to understand. Unless you are processing an array of strings in a update loop or something wild, you won’t notice much performance difference. In the event, you are processing alot of strings in a update loop, I would advise taking a step back and re-design that portion.
My intuition is that regex is significantly slower than (sub)string comparison. The performance impact will depend on how often you make the comparison.
Realistically you should just use the first thing that makes sense and works. Agonizing over performance before you’ve identified a bottleneck is the first and most grave of premature optimization offenses.