I'm kind of new to optimization and I'm in a friendly contest to code the fastest algorithm to find a square in a field with obstacles. I think my algorithm is pretty fast (2.07s in a 10k * 10k grid with 70% density) but I'd like to make it faster. My most used function is the one finding the max size for given coordinates in the square. Here it is :
int max_size(int i, int j, int **tab, int offset)
{
int a;
a = offset;
if (i > 0 && j > 0)
{
while (i + a < g_ymax && j + a < g_xmax && tab[i + a][j + a] - tab[i + a][j - 1] - tab[i - 1][j + a] + tab[i - 1][j - 1] == 0)
a++;
return (a);
}
else if (i > 0 && j == 0)
{
while (i + a < g_ymax && j + a < g_xmax && tab[i + a][j + a] - tab[i - 1][j + a] == 0)
a++;
return (a);
}
else if (i == 0 && j > 0)
{
while (i + a < g_ymax && j + a < g_xmax && tab[i + a][j + a] - tab[i + a][j - 1] == 0)
a++;
return (a);
}
else
{
while (i + a < g_ymax && j + a < g_xmax && tab[i + a][j + a] == 0)
a++;
return (a);
}
}
So this is very ugly and I'd like to optimize it. Would make it an inline function help or not at all ? I tried to run some tests but I didn't see much differences.