gpt4 book ai didi

c++ - bool判断这么慢?

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:21:03 26 4
gpt4 key购买 nike

<分区>

我正在优化函数,我尝试了各种方法甚至sse,并修改了代码以从不同的位置返回以查看计算时间跨度但最后我发现大部分时间都花在了bool判断上。即使我用一个简单的添加操作替换了 if 语句中的所有代码,它仍然花费 6000 毫秒。

我的平台是 gcc 4.7.1 e5506 cpu。它的输入'a'和'b'是一个1000size的int数组,'asize','bsize'是对应的数组大小。 MATCH_MASK = 16383,我运行函数100000次来统计一个时间跨度。这个问题有什么好主意吗。谢谢!

   if (aoffsets[i] && boffsets[i])  // this line costs most time

代码:

uint16_t aoffsets[DOUBLE_MATCH_MASK] = {0}; // important! or it will only be right on the first time
uint16_t* boffsets = aoffsets + MATCH_MASK;
uint8_t* seen = (uint8_t *)aoffsets;
auto fn_init_offsets = [](const int32_t* x, int n_size, uint16_t offsets[])->void
{
for (int i = 0; i < n_size; ++i)
offsets[MATCH_STRIP(x[i])] = i;
};
fn_init_offsets(a, asize, aoffsets);
fn_init_offsets(b, bsize, boffsets);

uint8_t topcount = 0;
int topoffset = 0;
{
std::vector<uint8_t> count_vec(asize + bsize + 1, 0); // it's the fastest way already, very near to tls
uint8_t* counts = &(count_vec[0]);
//return aoffsets[0]; // cost 1375 ms
for (int i = 0; i < MATCH_MASK; ++i)
{
if (aoffsets[i] && boffsets[i]) // this line costs most time
{
//++affsets[i]; // for test
int offset = (aoffsets[i] -= boffsets[i]);
if ((-n_maxoffset <= offset && offset <= n_maxoffset))
{
offset += bsize;
uint8_t n_cur_count = ++counts[offset];
if (n_cur_count > topcount)
{
topcount = n_cur_count;
topoffset = offset;
}
}
}
}
}
return aoffsets[0]; // cost 6000ms

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com