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?
Answer by Jessespike
Oct 14, 2012 at 05:42 PM
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.
Not a perfect answer for the question, but good catch; this kind of things can be done different ways. So i need to redesign, and try to figure out a better solution.
In my opinion it's the perfect answer ;)
Performance always depends on several things. For example the implementation of RegEx can differ on different platforms (standalone / mobile / ...). It's also a huge difference on what hardware you run a certain piece of code. There's never a clear answer to such a question. The best way is to create a benchmark to test some implementations on the target hardware.
As long as you don't have real performance problems it's not really worth to optimise. If you have Unity pro, use the profiler to track down your bottlenecks.
Well you know: "Any difference?", "Probably, but i quess.." ;) Anyways good answer for my case.
I dont have Unity pro, so i tried Adobe Shark. Not sure yet how should i read the result, but i'm sure it will open up.
There is huge performance problem with other computer: fps drops from 90 to 5 for a moment. I dont think i can do nothing, but try to redesign all that might cause the problem. It'll be interesting later to check out the bottleneck by commenting out parts of the code..
EDIT: There were function call to other script, and in that function i had forget to comment out a print() call. I am pretty sure it was all about that.
Answer by flaviusxvii
Oct 17, 2012 at 10:01 PM
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.
You are right about that. Anyway, my problem (probably) was just a silly mistake; print() call :) So i guess i'm fine to use substring or regex ..or even both ten times without any problem.
Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.
The best place to ask and answer questions about development with Unity.
To help users navigate the site we have posted a site navigation guide.
If you are a new user to Unity Answers, check out our FAQ for more information.
Make sure to check out our Knowledge Base for commonly asked Unity questions.
If you are a moderator, see our Moderator Guidelines page.
We are making improvements to UA, see the list of changes.
Answers and Comments
12 People are following this question.
Is there a way to limit GUI.TextField to numbers only?
Bypassing special characters
"String too long for TextMeshGenerator. Cutting off characters." when doing Regex.Replace
How to use the Regex class to check for string with case insensitive?