Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by chelnok · Oct 14, 2012 at 05:34 PM · regex

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?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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.

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image chelnok · Oct 17, 2012 at 08:12 PM 0
Share

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.

avatar image Bunny83 · Oct 17, 2012 at 08:57 PM 0
Share

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.

avatar image chelnok · Oct 17, 2012 at 09:42 PM 0
Share

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.

avatar image
0

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image chelnok · Oct 17, 2012 at 10:32 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

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

Answers Answers and Comments

12 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges