更新: 我刚刚成功打破了我的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代码更快,而且更加紧凑。
有没有办法让上述代码更快?(使用一个字符数组并尝试查找字符出现的位置)
我写了一些代码,我真的很困惑。
初始化:
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个if语句的情况下实现相同的性能?
编辑1:如果我创建64个if语句,与32个if语句版本相比,速度没有增加。
编辑2:如果我删除else并保留if语句,程序需要4秒钟才能完成。
那么,如何解释上述不合理的结果?
file_char[0] == to_find
,那么它将打印“found”并跳过接下来的32个。你应该用if
替换所有的else if
。 - Jabberwockyelse
的版本的执行时间是多少呢?我敢打赌它更接近于3.5秒而不是1.8秒。 - Jabberwocky