Hi I was just wondering while working on a C# script if one of two methods for variable retrieval is faster than the other. When trying to get the average color from a texture I have two slightly different ways of going about it, mainly just either storing a variable or retrieving it.
This is what I am doing. Example 1:
Color[] Colors = texture.GetPixels();
int pixelCount = Colors.Length;
int r =0; int g = 0; int b = 0; int a = 0;
for (int i = 0; i < pixelCount; i++)
{
Color c = Colors*;*
r += c.r;
g += c.g;
b += c.b;
if (includeAlpha)
a += c.a;
}
if (a != 0)
a /= pixelCount;
return new Color (r/pixelCount, g/pixelCount, b/pixelCount, a);
Example 2:
Color[] Colors = texture.GetPixels();
int pixelCount = Colors.Length;
int r =0; int g = 0; int b = 0; int a = 0;
for (int i = 0; i < pixelCount; i++)
{
r += Colors*.r;*
g += Colors*.g;*
b += Colors*.b;*
if (includeAlpha)
a += Colors*.a;*
}
if (a != 0)
a /= pixelCount;
return new Color (r/pixelCount, g/pixelCount, b/pixelCount, a);
So there you can see the only change is in the looping ‘for’ statement. By storing the color in a variable or accessing it for every integer value, is one a better option than the other? If the reasoning could also be explained I would appreciate it.