- Home /
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]);
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
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.
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.
}
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.
@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.
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.
@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.
@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 ;)