对于快速 MTF(http://en.wikipedia.org/wiki/Move-to-front_transform),我需要更快的版本将 char 从数组内部移动到它的前面:
char mtfSymbol[256], front;
char i;
for(;;) { \\ a very big loop
...
i=get_i(); \\ i is in 0..256 but more likely to be smaller.
front=mtfSymbol[i];
memmove(mtfSymbol+1, mtfSymbol, i);
mtfSymbol[0]=front;
}
cachegrind 显示,对于 memmove,这里有很多分支错误预测。
对于其他版本的代码(不是第一个示例中的 memmove,而是这个)
do
{
mtfSymbol[i] = mtfSymbol[i-1];
} while ( --i );
有很多字节读取/写入、条件分支和分支错误预测
i 不是很大,因为它是用于“良好”输入的 MTF - BWT 之后的文本文件(Burrows–Wheeler 变换)
编译器是 GCC。