I wrote a piece of code that needs to be optimized. Just felt like checking with community to see if that code is indeed optimal. It fills up the accumulator for the Hough transform. I actually just copy pasted most the code from the OpenCV library. Thanks!
int i,j,n,index;
for (i = 0;i<numrows;i++)
{
for (j = 0;j<numcols;j++)
{
if (img[i*numcols + j] == 100)
{
for (n = 300;n<600;n++)
{
index = cvRound(j*tabCos[n] + i * tabSin[n]) + (numrho-1)/2;
accum[(n+1) * (numrho+2) + index+1]++;
}
}
}
}
No it's not. Replace as many of the []
usages as you can by simple pointer arithmetic to iterate the arrays in question. Abstract out invariant expressions into local variables.
However, the first question is, does your profiler show that this code is a bottleneck in the context of your entire app. If not, why bother micro-optimizing this?
EDIT: loop micro-optimization - prefer the second as no array indexing required (mult versus add)
int ints[100];
int i;
int *pi;
for (i = 0; i < 100; ++i)
{
printf("%d", ints[i]);
}
for (pi = ints; pi < ints + 100; ++pi)
{
printf("%d", *pi);
}