Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Need help for search optimization

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?

like image 900
Mama Tate Avatar asked Dec 09 '13 14:12

Mama Tate


People also ask

Can I do SEO optimization myself?

You can absolutely do SEO yourself or DIY SEO (Do It Yourself SEO). With some research and lots of practice, anyone can learn how to do SEO for their business. A quick way to get started with SEO is to enter your URL here and then focus your SEO efforts on the recommended action items.

What's the easiest thing someone can do to boost their SEO?

Make sure your website is user-friendly Make sure it's immediately clear to visitors who you are and what you're offering. Use a lot of headings to improve readability, and put your keywords in the headings. Headings stand out for search engines and will help you rank higher. Site speed is also important for SEO.


1 Answers

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.

like image 85
Roy Dictus Avatar answered Sep 23 '22 20:09

Roy Dictus