优化前:
int x, y;
unsigned long offset;
unsigned char r, g, b, mi, ma;
for(y = 0; y < HEIGHT; y ++)
for(x = 0; x < WIDTH; x ++)
{
offset = y * WIDTH + x;
b = in[offset * 3];
g = in[offset * 3 + 1];
r = in[offset * 3 + 2];
mi = min(r, min(g, b));
ma = max(r, max(g, b));
if(((unsigned short)ma + (unsigned short)mi) > THRESHOLD * 2)
out[offset] = 255;
else
out[offset] = 0;
}
优化后:
int x, y;
unsigned long offset = 0, p;
unsigned char r, g, b, mi, ma;
for(y = 0; y < HEIGHT; y ++)
for(x = 0; x < WIDTH; x ++)
{
p = ((unsigned long *)in)[offset];
b = ((unsigned char *)(&p))[0];
g = ((unsigned char *)(&p))[1];
r = ((unsigned char *)(&p))[2];
mi = min(r, min(g, b));
ma = max(r, max(g, b));
if(((unsigned short)ma + (unsigned short)mi) > THRESHOLD * 2)
out[offset] = 255;
else
out[offset] = 0;
offset ++;
}
优化方式:
1、把原来每个点占3个字节(R, G, B)的存储方式改为4个字节存储方式,这样的话访存时可以对齐。
2、用一个long型变量一次取一个点,再分给R, G, B,避免多次访存操作。
3、offset那里稍改了一下,其实对性能没什么影响。
200字以内,仅用于支线交流,主线讨论请采用回复功能。