有这么两个函数,如下:
int f1(uchar* D, int W, double x0, double y0, double Vec[2], int Length) { int S = 0; double x = x0, y = y0; double VecX = Vec[0]; double VecY = Vec[1]; for ( int i=0; i<=Length; i++ ) { int ix = (int)x; int iy = (int)y; //if(ix>0 && iy>0) 【注释1】 S+=D[iy*W+ix]; x+=VecX; y+=VecY; } return S; }
int f2(uchar* D, int W, double x0, double y0, double Vec[2], int Length) { int S = 0; __int64 BB = 4194304;//2^22 __int64 x = (__int64)(x0*BB); __int64 y = (__int64)(y0*BB); __int64 VecX = (__int64)(Vec[0]*BB); __int64 VecY = (__int64)(Vec[1]*BB); for ( int i=0; i<=Length; i++ ) { int ix = x>>22; int iy = y>>22; //if(ix>0 && iy>0) 【注释2】 S+=D[iy*W+ix]; x+=VecX; y+=VecY; } return S; }
运行时,
//输入参数如下: //D对应一张大小为2448x2048的灰度图数据 int W = 2448; double x0 = 23; double y0 = 23.6; double a = 69*3.141592653/180; double Vec[2] = {cos(a), sin(a)}; int Length = 500;
运行1000次,各个函数耗时情况如下:
函数 | 不启用注释【1】【2】 | 启用注释【1】【2】 |
f1 | 0.848ms | 0.009ms |
f2 | 0.003ms | 0.597ms |