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?

more ▼

asked Oct 14, 2012 at 05:34 PM

avatar image

361 20 18 24

(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

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.

more ▼

answered Oct 14, 2012 at 05:42 PM

avatar image

2.2k 46 41 49

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.

Oct 17, 2012 at 08:12 PM chelnok

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.

Oct 17, 2012 at 08:57 PM Bunny83

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.

Oct 17, 2012 at 09:42 PM chelnok
(comments are locked)
10|3000 characters needed characters left

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.

more ▼

answered Oct 17, 2012 at 10:01 PM

avatar image

5.7k 48 43 75

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.

Oct 17, 2012 at 10:32 PM chelnok
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here



Answers and Comments



asked: Oct 14, 2012 at 05:34 PM

Seen: 1308 times

Last Updated: Oct 17, 2012 at 10:32 PM