I am running an implementation of something, fully built for efficiency. I am not that experienced on this topic yet, and would like to know when to declare variables best. The following part of my code particularly:
//Variables not declared in the next part are declared here (like xx, y1, x1.....)
for(s = 0; s < this.Width; s++)
{
y = ymin;
for(z = 0; z < this.Height; z++)
{
x1 = 0;
y1 = 0;
looper = 0;
while(looper < curMaxIter && Math.Sqrt((x1 * x1) + (y1 * y1)) < 2)
{
looper++;
xx = (x1 * x1) - (y1 * y1) + x;
y1 = 2 * x1 * y1 + y;
x1 = xx;
}
double perc = looper / (double)curMaxIter;
int val = ((int)(perc * 255));
b.SetPixel(s,z,cs[val]);
y += intigralY;
}
x += intigralX;
}
As you all can imagine, this while loop continues for a while... and i want to reduce the time it takes as much as possible in any way, while the result will be the same. This whole part of code is again run for thousands of times per frame (I'm rendering Mandelbrot images for those curious). My main question: is it faster to declare variables (like perc and val, but also like xx, y1 and x1) right before i use them? (like perc and val) or better declare them before the whole loops? (Like i did with xx, y1 and x1 etc.)