Need help for search optimization

c#, optimization

Solution

You could create a 3-dimensional array so that you can look up a tile at a specific location by just looking what's in `Tiles[x, y + 1, z]`.

You can then iterate through your data in 2 loops: one to build up Tiles and one to do the checks you are doing in your code above, which would then just be:

for(int i = 0; i < Tiles.Length; i++)
{
    Tile toFind = Tiles[Tile[i].x, Tile[i].y + 1, Tile[i].z];
    if (toFind != null) ...
}

You would have to dimension the array so that you have 1 extra row in the y so that `Tiles[x, y + 1, z]` doesn't cause an index-out-of-range exception.

Problem

I am fairly new to programming and i need some help with optimizing. Basically a part of my method does: ``` for(int i = 0; i < Tiles.Length; i++) { x = Tiles[i].WorldPosition.x; y = Tiles[i].WorldPosition.y; z = Tiles[i].WorldPosition.z; Tile topsearch = Array.Find(Tiles, search => search.WorldPosition == Tiles[i].WorldPosition + new Vector3Int(0,1,0)); if(topsearch.isEmpty) { // DoMyThing } } ``` So i am searching for a Tile in a position which is 1 unit above the current Tile. My problem is that for the whole method it takes 0.1 secs which results in a small hick up..Without `Array.Find` the method is 0.01 secs. I tried with a for loop also, but still not great result, because i need 3 more checks for the bottom, left and right.. Can somebody help me out and point me a way of acquiring some fast results? Maybe i should go with something like threading?

Original source

Related problems