- Home /
Performace Overhit for String Concatenation in Online Pool Game Posted
Hi,
guys plz help me regarding long string concatenation!
i m making online pool game in which for pool ball movement sync to other players, i m broadcasting player(whoes turn is ) to other players and applying to other player balls!
when i m doing string concatenation i results in heavy performance overhit and ball jittering much! i m just sending data 10 times/second,
below are some code snippest i tried,
StringBuilder sbData = new StringBuilder(10000);
if (timeLastSending >= sendingPeriod)
{
foreach(KeyValuePair<string, GameObject> kv in playerBalls)
{
if(kv.Value.rigidbody.velocity.magnitude > 0.005f)
{
kv.Value.rigidbody.useGravity = true;
sbData.AppendFormat("{0:.000000},{1:.000000},{2:.000000},{3:.000000},{4:.000000},{5:.000000},{6},",
kv.Value.transform.position.x, kv.Value.transform.position.y, kv.Value.transform.position.z,
kv.Value.transform.position.x, kv.Value.transform.position.y, kv.Value.transform.position.z, kv.Key);
//print("SEND");
gameInstance.PlayerMoveMade(sbData.ToString());
sbData.Remove(0, sbData.Length);
}
}
timeLastSending = 0;
return;
i also tried for this one..
if(gameRulesScript.isGameStart && gameRulesScript.isSendBallData) // for the player whose' turn is
{
for(short i = 0; i < 15; i++)
{
if(balls[ballNo] != null)
{
balls[ballNo].rigidbody.useGravity = true;
data = string.Format("ball{0:D2},{1:.000000},{2:.000000},{3:.000000},{4:.000000},{5:.000000},{6:.000000},", ballNo + 1, balls[ballNo].transform.position.x, balls[ballNo].transform.position.y, balls[ballNo].transform.position.z,
balls[ballNo].transform.localEulerAngles.x, balls[ballNo].transform.localEulerAngles.y, balls[ballNo].transform.localEulerAngles.z);
StartCoroutine("Move1");
}
ballNo++;
if(ballNo > 14) ballNo = 0;
}
}
on opponent side also when i m spliting it it takes lots of load !
please help me, is their any other better way so that this can be achieved at no performance overhit and game runs sooth like any other pool game!
Thankx!
Off the top of my head I can't think of a better method but two things come to $$anonymous$$d. Do you need 6 digit precision? The shorter the better. Also, cache balls[ballNo].transform so it doesn't need to read balls[ballNo] a half a dozen times. One other thought, how about converting the data to a bit stream?
thanks!
actually here 6 digit precision is needed for accurate position, i didn't get about caching of balls[ballNo].transform, can you please elaborate it?
i will try for conversation of sting to byte stream(as back end server supports byte format)!
Its a $$anonymous$$or thing but if you do: Transform ballTransform = balls[ballNo].transform; ... transform.position.x, transform.position.y etc Then the computer only needs to read the value stored in balls[ballNo] once (for each ball, each time that code is called). Come to think of it, the compiler might do it for you. But its good to know.
Your answer
Follow this Question
Related Questions
1 UI Text vs 2 UI Text 1 Answer
String.Equals returning false? 2 Answers
Grab part of a string by looking for keyword 2 Answers
How to extract part of a string variable? 1 Answer
Does Unity's Mono do a reference compare in its string == operator? 2 Answers