图像处理的简单优化-03:优化-消除临时变量

消除临时变量

注意到在灰度计算宏中,有临时变量的使用,如下所示:


#define	GRAY_PIXEL(ptr, gray)	\
{ \
	ptr++; \
	int r = *ptr, g = *(ptr + 1), b = *(ptr + 2); \
	gray = RGB2GRAY(r, g, b); \
	*ptr++ = gray; \
	*ptr++ = gray; \
	*ptr++ = gray; \
}

尝试将其转换为如下方式以消除临时变量:

#define	GRAY_PIXEL(ptr, gray)	\
{ \
	ptr++; \
	gray = RGB2GRAY(*ptr, *(ptr + 1), *(ptr + 2)); \
	*ptr++ = gray; \
	*ptr++ = gray; \
	*ptr++ = gray; \
}

再次测试,结果如下:

Done.
17, 17, 17, 17, 17, 18, 17, 18, 17, 17, 17, 17, 17, 17, 17, 17, 
17, 17, 18, 17, 17, 17, 17, 18, 18, 17, 18, 17, 17, 17, 17, 17, 
18, 17, 17, 17, 17, 17, 17, 17, 17, 18, 17, 17, 17, 17, 982, 17, 
17, 17, 17, 18, 17, 18, 17, 17, 17, 17, 18, 18, 17, 17, 17, 18, 
17, 17, 17, 17, 17, 17, 18, 17, 17, 17, 17, 17, 17, 17, 17, 17, 
17, 17, 17, 18, 17, 17, 17, 17, 17, 17, 17, 18, 17, 18, 17, 17, 
17, 17, 17, 19, 17, 17, 982, 17, 17, 17, 17, 17, 17, 17, 18, 17, 
18, 17, 18, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 

Average:17 ms
Max:19 Min:17 ms
(Max + Min)/2=18 ms

貌似没有多大改进。看来编译器已经将这个频繁使用的变量优化掉了。



你可能感兴趣的:(C++,优化,性能,图像处理)