没有裂变的代码如下所示:
int check(int * res, char * map, int n, int * keys){
int ret = 0;
for(int i = 0; i < n; ++i){
res[ret] = i;
ret += map[hash(keys[i])]
}
return ret;
}裂变:
int check(int * res, char * map, int n, int * keys){
int ret = 0;
for(int i = 0; i < n; ++i){
tmp[i] = map[hash(keys[i])];
}
for(int i = 0; i < n; ++i){
res[ret] = i;
ret += tmp[i];
}
return ret;
}备注:
map[hash(keys[i])]。if(tmp[i]) res[ret++] = i;来避免if,我使用的是ret += tmp[i]。map[..]总是0或1裂变版本通常要快得多,我想解释一下原因。我最好的猜测是,ret += map[..]仍然引入了一些依赖项,从而防止了投机性的执行。
我想知道是否有人有更好的解释。
发布于 2012-06-20 16:13:36
我不认为是数组索引,而是对函数hash()的调用可能导致管道阻塞,并阻止优化的指令重新排序。
https://stackoverflow.com/questions/11123658
复制相似问题