非常慢的数组索引 [英] very slow array indexing

查看:122
本文介绍了非常慢的数组索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

当我访问C数组中的随机位置时,性能会严重下降.因此,我有一个简短的功能:

Hello everyone,

I have a severe performance decrease when I am accessing random locations in a C-array. So, I have a function which is briefly as follows:

template<typename t="">
int fill_histogram(T *hist)
{
   for (int i = 0; i < num_pixels; ++i)
   {
       valid = true;
       if (some_checks) valid = false;
       if (valid) hist[some_index + (another_index *offset)]++;
   }
}
</typename>



现在,此过程的500次迭代大约需要130秒.当我将最后一行替换为类似的内容时:



Now, 500 iterations of this take about 130 seconds. When I replace the last line with something like:

if (valid) hist[0]++;



计算时间降至30秒.

当我检查条件是否有效时,将在循环内同时计算索引.我很确定没有什么东西可以优化了,而且数组索引似乎真的很慢.

我可以做些什么来加快速度吗?

谢谢,

Keith



The computation time drops to 30 sec.

The indexes are calculated within the loop at the same time as when I check if the conditions are valid. I am pretty sure nothing is getting optimized away and the array indexing seems to be really slow.

Can I do something to speed this up?

Thanks,

Keith

推荐答案

如果索引为零,则将删除一个乘法和一个加法(如果您对指针算术中执行的加法计数,则可能是第二个加法,每次迭代都在引擎盖下发生),这可能是造成速度差异的原因.
您可以在for循环之外计算 another_index *偏移量吗?还是使用加法而不是乘法?可能会帮助您加快速度.
If you index by zero, you are removing a multiply and an addition (and perhaps a second addition if you count the addition performed in the pointer arithmetic that may be occurring under the hood) per iteration, which may account for the difference in speed.

Can you calculate the another_index * offset outside of the for loop? Or use additions instead of multiplications? That might help you speed things up.




有点棘手.

因此,要详细说明该功能,请执行以下操作:

Hi,

It''s a bit tricky.

So, to elaborate the function is something as follows:

template<typename t="">
int fill_histogram(T *hist)
{
   for (int i = 0; i < num_pixels; ++i)
   {
       valid = true;
       for(int j = 0; j < someVal; ++j)
       {
          if (some_checks) { valid = false; break; }
          index_1 += value * offset_1[j]);
       }
       for(int j = 0; j < someOtherVal; ++j)
       {
          if (some_checks) { valid = false; break; }
          index_2 += value * offset_2[j]);
       }

       if (valid) hist[index_1 + (index_2 *some_offset)]++;
   }
}
</typename>



我将尝试进行一些简化,但是仍然很惊讶这些操作几乎将计算时间增加了3倍.没想到它会这么慢.

干杯,
基思(Keith)



I will try and do some simplifications but still quite surprised that these operations almost increase the computation time 3 fold. Was not expecting it to be that slow, somehow.

Cheers,
Keith


好吧,事实证明,真正的罪魁祸首是这条线!

Ok, it turns out that the real culprit is this line!

<br />
index += static_cast<int>(floorf((float)values[i]) * offsets[i]);<br />



这条线似乎使事情停顿了下来...似乎有些过分,我同意,但是floorf函数会变慢吗?还有更好的方法吗?

谢谢,
K



This line seems to slow things down to a halt...Seems excessive, I agree but is the floorf function that slow? Any better way to do this?

Thanks,
K


这篇关于非常慢的数组索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆