Optimizing a function with nested loops and if condition private  void find_tile(float[] x, float[] z, int nRigidBodies)  {
    int serial[]=new int[nRigidBodies];
    int numTiles=4;
    for(int i=0;i&lt;nRigidBodies;i++)
    {
        // Check for the loaction of each tile to find which one should be switched on...
        //currently only supports 4
        for(int j=0;j&lt;numTiles;j++)
        {
            if ((x[i] &gt; tileCoords[j][0]) &amp;&amp; (x[i] &lt; tileCoords[j][1]) &amp;&amp; (z[i] &gt; tileCoords[j][2]) &amp;&amp; (z[i] &lt; tileCoords[j][3]))
            {
                serial[i]=j+1;
                break;
            }
        }
    }


We have a optitrack system that tracks objects in a room and the number of objects is represented by nRigidBodies and their x-y cordinate by the x[] and z[] array passed here.

Also we have a fixed number of Tiles which are fixtures on the ceiling represented by numTiles and the bounds of these tiles are in tileCoords.

If an object is directly under one of these fixtures, we switch the fixture on.So I create an array serial[] which is of the same size as nRigidBodies.For every object we check if is is within the bounds of that fixture and if it we assign the fixture number to the serial array.Later we just take out all the fixture number from the array( coz these should only be lit up ) and send them a message to light up and the ones not in here are switched off.

The condition where the object is not within any of the fixtures is fine since it is checked later in the code and taken care of.

Sample Input    
    float[] x= {1.5, -0.5, 3.5}
    float[] z= {2.5, -1.5, 3.2}
    nRigidBodies= 3
    numTiles=4
    tileCoords={{0.5,1.5,1.5,2.5},{-0.5,0.5,0.5,1.5},{0.5,1.5,0.5,1.5},{-0.5,0.5,-0.5,0.5}}

Expected Output
   serial[]={1,2,0}


I have a simple function that checks for if each object is within the bounds of the cordinates or not.If it is, we assign the number to a variable.

Currently the inner loop generally only has only 4 iterations so it doesn't matter much.However in the future it might run for upto 50 iterations with the outer loop running for 5 iterations max.The code is a time sensitive piece which controls lights in real time.Too much time in computing this function would result in lights appearing to be lagged.

It would be of great help if someone can guide me on how to optimize the function above for faster computation.

I was thinking of using threads to run all of the outer loops at once.If there is something better which can be achieved with single threaded model,I would love to stick to it as that might be making it really complicated.
Disclaimer: I have not tested any of these optimizations. Some of these optimizations could potentially even harm the speed - or they could already be covered by the JVM. Do your own benchmarking.

Helping the Compiler/JVM
If you don't need to override this method, declare it final. The compiler might inline the method if it thinks it helps. Additionally, declare constants as final - such as numTiles.

Preventing repeated lookups
You need to access x[i], tilecoords[j], z[i] and such multiple times. Consider storing those in temporary variables. Don't forget to test to see if it is actually faster to do so. An example (with really bad variable names - you might want to pick better names than xi and zi

for(int i=0;i&lt;nRigidBodies;i++)
{
    // Check for the loaction of each tile to find which one should be switched on...
    //currently only supports 4
    float xi = x[i];
    float zi = z[i];
    for(int j=0;j&lt;numTiles;j++)
    {
        if ((xi &gt; tileCoords[j][0]) &amp;&amp; (xi &lt; tileCoords[j][1]) &amp;&amp; (zi &gt; tileCoords[j][2]) &amp;&amp; (zi &lt; tileCoords[j][3]))
        {
            serial[i]=j+1;
            break;
        }
    }
}


Fixing bugs
int serial[]=new int[nRigidBodies]; creates a new int array with a size of nRigidBodies. int's default value is 0. Thus, the entire array will be filled with 0. Given that your innerloop starts at index 0 and sets the "found" index into the array, it's possible it's setting 0 over the default value. That's not so important. What is important is the reverse implication: If the serial array contains a 0 at a given index, you do not know whether this is because no tile-index was found, or because the tile-index that was found was the first one.