- Home /
Substring vs Regex
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: http://answers.unity3d.com/questions/202004/substrings.html http://answers.unity3d.com/questions/124160/how-to-use-the-regex-class-to-check-for-string-wit.html
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.
Your answer
Follow this Question
Related Questions
Substring Exception 1 Answer
How to replace a word inside a string with different white space combos? 0 Answers
Allow only numbers in an input field 1 Answer
Creating a card game. Best ways to interpret the various cards? 1 Answer
Replace() with string and Regex not replacing numbers/integers 2 Answers