gpt4 book ai didi

c++ - 低级 C/C++ 性能?

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:50:59 28 4
gpt4 key购买 nike

更新:如果代码:我刚刚成功击败了自己的 32:

void test(char *file_char, unsigned int size)
{
char* file_ = file_char;
char* size_x = file_char+size;
char to_find = 0;

for(unsigned int i = 0; i < 10000; i++)
{
file_char = file_;

while(*file_char++ != to_find);//skip all characters till we find a 0

if(*file_char)//some if in order to avoid compiler removing our test code
cout << "found";
}
}

上面的代码要求 0 在数组中至少出现一次,否则会出现错误,但它比 if 代码快一点,而且更紧凑。

有没有办法让上面的代码更快?(有一个 char 数组并试图找到一个 char 出现的位置)?

我写了一些代码,我真的很困惑。

初始化:

int main()
{
FILE *file;
file = fopen("C:\\data.txt", "rb");

static const int size = 60000;

char *file_char = (char*)malloc(size);

unsigned int i = 0;
while(i < size)
fread(&file_char[i++], 1, 1, file);

clock_t clock_ = clock();
test(file_char, size);
std::cout << ((double)clock()-clock_)/1000;
return 0;
}

下面的代码执行需要 3.5 秒:

void test(char *file_char, unsigned int size)
{
for(unsigned int i = 0; i < 100000; i++)
{
unsigned int pos = 0;
char to_find = 0;
while(pos < size)
if(file_char[pos++] == to_find)
std::cout << "found";
}
}

但下面的代码需要 1.8 秒,时间减半!

void test(char *file_char, unsigned int size)
{
for(unsigned int i = 0; i < 100000; i++)
{
unsigned int pos = 0;
char to_find = 0;
while(pos < size)
{
if(file_char[pos] == to_find)
std::cout << "found";
else if(file_char[pos+1] == to_find)
std::cout << "found";
else if(file_char[pos+2] == to_find)
std::cout << "found";
else if(file_char[pos+3] == to_find)
std::cout << "found";
else if(file_char[pos+4] == to_find)
std::cout << "found";
else if(file_char[pos+5] == to_find)
std::cout << "found";
else if(file_char[pos+6] == to_find)
std::cout << "found";
else if(file_char[pos+7] == to_find)
std::cout << "found";
else if(file_char[pos+8] == to_find)
std::cout << "found";
else if(file_char[pos+9] == to_find)
std::cout << "found";
else if(file_char[pos+10] == to_find)
std::cout << "found";
else if(file_char[pos+11] == to_find)
std::cout << "found";
else if(file_char[pos+12] == to_find)
std::cout << "found";
else if(file_char[pos+13] == to_find)
std::cout << "found";
else if(file_char[pos+14] == to_find)
std::cout << "found";
else if(file_char[pos+15] == to_find)
std::cout << "found";
else if(file_char[pos+16] == to_find)
std::cout << "found";
else if(file_char[pos+17] == to_find)
std::cout << "found";
else if(file_char[pos+18] == to_find)
std::cout << "found";
else if(file_char[pos+19] == to_find)
std::cout << "found";
else if(file_char[pos+20] == to_find)
std::cout << "found";
else if(file_char[pos+21] == to_find)
std::cout << "found";
else if(file_char[pos+22] == to_find)
std::cout << "found";
else if(file_char[pos+23] == to_find)
std::cout << "found";
else if(file_char[pos+24] == to_find)
std::cout << "found";
else if(file_char[pos+25] == to_find)
std::cout << "found";
else if(file_char[pos+26] == to_find)
std::cout << "found";
else if(file_char[pos+27] == to_find)
std::cout << "found";
else if(file_char[pos+28] == to_find)
std::cout << "found";
else if(file_char[pos+29] == to_find)
std::cout << "found";
else if(file_char[pos+30] == to_find)
std::cout << "found";
else if(file_char[pos+31] == to_find)
std::cout << "found";

pos+=32;
}
}
}

我使用的是 Visual Studio 2012 x64,程序从不计算任何东西,因为没有 char 是 0。这怎么解释?如何在不使用 32 个 ifs 的情况下存档相同的性能?

编辑 1:如果我创建 64 个 ifs,与 32 个 ifs 版本相比没有速度提升。

编辑 2:如果我删除 else 并保留 ifs,程序需要 4 秒。

现在,如何解释上述不合理的结果?

最佳答案

您的循环基本上由两个比较组成:pos < sizefile_char[pos] == to_find .通过展开循环,您将比较次数从 2 * size 减少到 (size + size/32)。

关于c++ - 低级 C/C++ 性能?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22072858/

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