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 Chris Walker · Aug 29, 2013 at 08:27 AM · slowing-down

slow running script with string.split()

I am trying to speed up a script but it is running extremely slow. I suspect it is from string.split(); Does anyone know if there is a faster way to split a string?

     public void updateFromDragon(){
     
     dragonArray =strIn.Split(',');
     
         jaw = dragonArray[1];
         tempreture = dragonArray[2];
         humidity = dragonArray[3];
         headX = toFloat(dragonArray[4]);
         headY = toFloat(dragonArray[5]);
         headZ = toFloat(dragonArray[6]);
     
Comment
Add comment · Show 2
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 smrt_co · Aug 29, 2013 at 08:45 AM 0
Share

Have you used Unity profiler to deter$$anonymous$$e if the slowdown is indeed caused by this bit of code?

If that is the case, maybe this will help:

http://stackoverflow.com/questions/568968/does-any-one-know-of-a-faster-method-to-do-string-split

avatar image ArkaneX · Aug 29, 2013 at 09:30 AM 1
Share

I don't think this is Split issue. If you're able, try profiling as @smrt_co suggests. If you can't profile, then maybe you could post a bit more of your code.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by gjf · Aug 29, 2013 at 09:37 AM

.Split isn't the best solution for this.

instead of using strIn (is that global?), try passing the parameters in a class/structure so that you don't need to pack/unpack the required variables - it's much more efficient.

your class would look something like:

 public class DragonClass
 {
     public string Jaw;
     public float Temperature;   // Probably needs this as a float (or perhaps int)
     public float Humidity;      // Same here...
     public float HeadX;
     public float HeadY;
     public float HeadZ;
 }

then to access them it's something like:

 public void updateFromDragon(DragonClass dragon)
 {
     Debug.Log("Jaw is " + dragon.Jaw);
     Debug.Log("Temperature is " + dragon.Temperature);
     etc.
 }
Comment
Add comment · Show 10 · 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 NonGamingCoder · Aug 29, 2013 at 09:43 AM 0
Share

I totally agree. Not enough reputation yet to up-vote, but String.Split is pretty expensive. I'm guessing that the other part of your code includes:

string value = val1 + val2 + val3; // Really expensive - includes 3 array reallocations.

Also, is the suffix "Class" needed? Prolly not... Readable code is good code.

avatar image gjf · Aug 29, 2013 at 09:51 AM 0
Share

@NonGa$$anonymous$$gCoder: i was trying to illustrate a point.

personally, i wouldn't use Class in the name because it's obvious to me, but it seems that our questioner might not be so familiar with classes, so this helps to reinforce it.

avatar image ArkaneX · Aug 29, 2013 at 10:46 AM 1
Share

And what if inStr is read from some external source, e.g. file? How is the OP supposed to initialize this DragonClass instance without splitting? Without knowing more, we can't provide much help.

As to Split performance - why do you say it's expensive? It might introduce overhead if it is called multiple times, but for example in my over 3 years old laptop (Intel i7 1.6Ghz) 10$$anonymous$$ splits of string with 6 comma separated elements executes in 11ms. I don't think it's expensive.

avatar image NonGamingCoder · Aug 29, 2013 at 10:49 AM 1
Share

@ArkaneX: 11ms, compared with sub 1ms for another structure. If the DragonClass is in a file, you can easily do something like this:

 [XmlRoot]
 public class DragonClass
 {
     [XmlAttribute] 
     public int DragonId; 

     [XmlElement] 
     public int Blah;
 }

Then you use serialization to get the data from the file. String splitting is pretty damn ugly, especially for this kind of thing.

With this scenario, we can possibly start loading the dragons from a database on the server and send it over the wire.

avatar image gjf · Aug 29, 2013 at 11:04 AM 0
Share

@ArkaneX: the cost is relative.

serializing will carry some overhead too, but only post/pre transmission and not all the time. it all depends on the context and we don't have enough info from the OP to know that, as you mentioned.

however, even if one were to have some other source/transmit over the interwebs/etc., string splitting is still unlikely to be the preferred method for your average developer. don't get me started on what an average developer is.

using a class is cleaner, simpler and more easily maintained/extended/etc. here endeth the sermon ;)

Show more comments

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

19 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

2D Character slows down in a trigger 2 Answers

Bullet slowing down while snapping to sphere normals 0 Answers

Input.ResetInputAxes Doesn't Stop From Moving? 0 Answers

moveposition() not working when slowing down the game!,rigidbody.moveposition not working when slowing down the game 0 Answers

Return to a scene rather than loading again 1 Answer


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