我最近编写了一些代码(ISO/ANSI C),并对它实现的糟糕性能感到惊讶。长话短说,原来罪魁祸首是floor()
函数。它不仅速度慢,而且没有矢量化(使用英特尔编译器,又名 ICL)。
以下是为 2D 矩阵中的所有单元格执行地板的一些基准:
VC: 0.10
ICL: 0.20
将其与简单的演员表进行比较:
VC: 0.04
ICL: 0.04
怎么可能floor()
比简单的演员阵容慢那么多?!它基本上做同样的事情(除了负数)。第二个问题:有人知道超快速的floor()
实现吗?
PS:这是我进行基准测试的循环:
void Floor(float *matA, int *intA, const int height, const int width, const int width_aligned)
{
float *rowA=NULL;
int *intRowA=NULL;
int row, col;
for(row=0 ; row<height ; ++row){
rowA = matA + row*width_aligned;
intRowA = intA + row*width_aligned;
#pragma ivdep
for(col=0 ; col<width; ++col){
/*intRowA[col] = floor(rowA[col]);*/
intRowA[col] = (int)(rowA[col]);
}
}
}