Luk*_*uka 5 c c++ micro-optimization
更新:我只是设法击败我自己的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";
}
}
Run Code Online (Sandbox Code Playgroud)
上面的代码要求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;
}
Run Code Online (Sandbox Code Playgroud)
下面的代码需要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";
}
}
Run Code Online (Sandbox Code Playgroud)
但是下面的代码需要1.8秒,HALF时间!
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;
}
}
}
Run Code Online (Sandbox Code Playgroud)
我使用的是Visual Studio 2012 x64,程序从不会cout任何东西,因为没有char是0.如何解释?如何在不使用32 ifs的情况下归档相同的性能?
编辑1:如果我创建了64个ifs,那么32 ifs版本的速度没有增加.
编辑2:如果我删除else并离开ifs程序需要4秒.
现在,如何解释上述不合理的结果?
我认为这两个代码是不同的.
在第一个中,您每次都会检查"if"比较.
在第二个,如果第一个是好的,你跳过以下所有!(因为其他)所以你节省了很多比较(但缺少支票).
要获得相同的代码,您必须删除所有"其他".
我做了一些测试来确定。
使用 g++(在 Linux 和 Windows 下),我得到与 Visual Studio 相同的结果:
版本 1(无显式循环展开)
g++ -O37.5秒
版本 2(显式循环展开)
g++ -O32.1秒
但打开-funroll-loops选项后(通常默认情况下不启用此优化,因为它可能会也可能不会使其运行得更快):
版本 1(无显式循环展开)
g++ -O3 -funroll-loops2.2秒
版本 2(显式循环展开)
g++ -O3 -funroll-loops2.2秒
所以这与循环展开有关。
编辑
您可以更改最后一个示例以显式插入哨兵,例如:
int main()
{
static const int size = 60000;
char *file_char = (char*)malloc(size+1); // The last element is the sentry
// ...Fill file_char[]...
file_char[size] = 0; // the sentry
// ...
}
Run Code Online (Sandbox Code Playgroud)
所以test函数不会失败(当然你必须检查是否找到了哨兵或“好”零,但这只是一个如果)。
版本 3(哨兵)
g++ -O30.68秒
g++ -O3 -funroll-loops0.72秒