Forum C# Code for converting array -> string running too long

Code for converting array -> string running too long

Postby wgfunstormcontest » November 23rd, 2012, 11:46 am

I generate a random tilemap on the server. When a player requests the map data, I convert the tilemap array to a string so I can send it to them. This works fine locally, but after uploading it to the playerio server I get a 'code ran too long' error when converting the array to a string.

My current tilemap is 150x100, so that basically means 15000 type conversions + string concatenations... I didn't really think this would be a problem. Am I doing something stupid here? Any ideas on how to fix it?

Code: Select all
int width = dungeonTiles.GetLength(0);
int height = dungeonTiles.GetLength(1);
String tilesString = "";
for (int row = 0; row < height; row++)
{
      for (int col = 0; col < width ; col++)
      {
            tilesString += (int)dungeonTiles[col, row];
      }
}
wgfunstormcontest
 
Posts: 55
Joined: September 25th, 2012, 8:26 pm

Re: Code for converting array -> string running too long

Postby Henrik » November 23rd, 2012, 12:44 pm

That doesn't sound like it should be a lot of work CPU-wise, so it might not be this piece of code that's causing it. I can immediately see two improvements though:

1) Why are you casting to int? If the type of dungeonTiles is int[,] this shouldn't be necessary.
2) Use a StringBuilder instead of concatenation.

So something like this:

Code: Select all
var dungeonTiles = new int[100,150];

//...

StringBuilder sb = new StringBuilder();
for (int row = 0; row < dungeonTiles.GetLength(1); row++) {
   for (int col = 0; col < dungeonTiles.GetLength(0); col++) {
      sb.Append(dungeonTiles[col, row]);
   }
}
var tileString = sb.ToString();
Henrik
.IO
 
Posts: 1880
Joined: January 4th, 2010, 1:53 pm

Re: Code for converting array -> string running too long

Postby wgfunstormcontest » November 23rd, 2012, 9:14 pm

I cast to int because the array is of type enum TILE_TYPES { GRASS, STONE, etc }

I benchmarked the speed using strinbuilder and += ... the results really surprised me!

Stringbuilder: 2ms (running server locally)
+=: 179ms!!!! (running server locally)

I googled it, and yea it seems like for large amounts of concatenations stringbuilder is really the way to go. I hadn't heard of it before, so thanks for introducing me to it!

Now that I changed to stringbuilder the error has disappeared and it's working again. Thanks!!
wgfunstormcontest
 
Posts: 55
Joined: September 25th, 2012, 8:26 pm

Re: Code for converting array -> string running too long

Postby dreamora » November 23rd, 2012, 10:37 pm

the problem is not the += per se.

The problem is that fact that you in this case have temporal string a + temporal string b = temporal string c with a and b becoming garbage right after that, requiring to be cleaned up by the GC.
Do that with some thousand objects and the GC will kick in which at such object numbers might easily take several dozen ms for the cleanup on its own.
dreamora
 
Posts: 225
Joined: March 2nd, 2012, 9:58 am


Return to C#



cron