C++ 指向二维数组中元素的指针会减慢代码速度
我有一段代码,可以访问关于x轴和y轴上的点的一些信息。此信息稍后用于在屏幕上绘制一些点。 这就是代码的工作原理:C++ 指向二维数组中元素的指针会减慢代码速度,c++,arrays,pointers,2d,slowdown,C++,Arrays,Pointers,2d,Slowdown,我有一段代码,可以访问关于x轴和y轴上的点的一些信息。此信息稍后用于在屏幕上绘制一些点。 这就是代码的工作原理: //MAX_X_COORD has a value of 169 //MAX_Y_COORD has a value of 55 void RedrawFromDATAtable() { COORD pos; HANDLE tmpbuf = CreateConsoleScreenBuffer(GENERIC_WRITE , NULL, NULL, CONSOLE_
//MAX_X_COORD has a value of 169
//MAX_Y_COORD has a value of 55
void RedrawFromDATAtable()
{
COORD pos;
HANDLE tmpbuf = CreateConsoleScreenBuffer(GENERIC_WRITE , NULL, NULL, CONSOLE_TEXTMODE_BUFFER, NULL);
WriteConsoleA(tmpbuf, " ", 1, NULL, NULL);
if(SetConsoleActiveScreenBuffer(tmpbuf)==0)
{MessageBoxA(NULL, "ERROR", "ERROR", 0);return;}
bufferdata_ex * dptr;
//bufferdata_ex * y_dptr;
int * lcol(new int); //Increases speed by reducing function calls - Experienced about twice as fast drawing!
for(short x=0;x<MAX_X_COORD;x++)
{
//y_dptr = bridge->DATA[x];
for(short y=0;y<MAX_Y_COORD;y++)
{
//dptr = (y_dptr+y); //Rewrite to use a constant pointer!
dptr = &(_bridge->DATA[x][y]);
if(dptr->InUse==true)
{
{
pos.X = x;
pos.Y = y;
SetConsoleCursorPosition(output, pos);
//haschanged = false;
}
if(!(*lcol==dptr->color)) //Need for a new color?
{ SetConsoleTextAttribute(output, dptr->color);lcol = &dptr->color;}
char c((char)dptr->sym);
WriteConsoleA(output, &c, 1, NULL, NULL);
lcol = &dptr->color;
}
}
}
SetConsoleTextAttribute(output, bridge->current_color);
SetConsoleCursorPosition(output, last_mouse_position);
SetConsoleActiveScreenBuffer(output);
CloseHandle(tmpbuf);
delete lcol;
}
除了代码的这一部分,其余部分是相同的 仅从组件上看,我们看到一个额外的mov(y_dptr的分配)。
看到在(外部)循环中的每次迭代中都是如何做到这一点的,并且代码中没有其他差异,这可能是性能下降的原因。
除此之外,您的代码中实际上没有任何东西可以利用您试图使用的指针魔法
f、 e.使用dptr=(y_dptr+y);通过直接在指针上使用增量(y_dptr++),可以丢失dptr或y_dptr
这是一些您没有使用的指针算术魔术,可以改进。我建议比较编译器在每种情况下生成的汇编程序。性能取决于许多因素,因此在抽象代码以简化问题时必须非常小心,正如您所做的那样。首先,你能给上面的代码段添加足够的代码,使其编译和运行,然后它是否会表现出同样的速度减慢?我希望你的优化器能比你更好地解决这个问题。Ram我明白你的意思,明天我一回到家访问我的文件,我就会更新这篇文章。就在这时,我有时间发表这篇文章。如果有人能通过查看当前代码来描述它为什么会变慢,那么我会非常感激它。好吧,我只是拿了这段代码并添加了一些伪定义来编译它(并使循环内表达式真正起作用),和clang在
-O2
处为两个函数生成了相同的代码。程序集不相同的原因是使用了y_dptr赋值,而第一个代码跳过了该赋值。
void RedrawFromDATAtable()
{
COORD pos;
HANDLE tmpbuf = CreateConsoleScreenBuffer(GENERIC_WRITE , NULL, NULL, CONSOLE_TEXTMODE_BUFFER, NULL);
WriteConsoleA(tmpbuf, " ", 1, NULL, NULL);
if(SetConsoleActiveScreenBuffer(tmpbuf)==0)
{MessageBoxA(NULL, "ERROR", "ERROR", 0);return;}
bufferdata_ex * dptr;
bufferdata_ex * y_dptr;
int * lcol(new int); //Increases speed by reducing function calls - Experienced about twice as fast drawing!
for(short x=0;x<MAX_X_COORD;x++)
{
y_dptr = _bridge->DATA[x];
for(short y=0;y<MAX_Y_COORD;y++)
{
dptr = (y_dptr+y); //Rewrite to use a constant pointer!
//dptr = &(bridge->DATA[x][y]);
if(dptr->InUse==true)
{
{
pos.X = x;
pos.Y = y;
SetConsoleCursorPosition(output, pos);
//haschanged = false;
}
if(!(*lcol==dptr->color)) //Need for a new color?
{ SetConsoleTextAttribute(output, dptr->color);lcol = &dptr->color;}
char c((char)dptr->sym);
WriteConsoleA(output, &c, 1, NULL, NULL);
lcol = &dptr->color;
}
}
}
SetConsoleTextAttribute(output, bridge->current_color);
SetConsoleCursorPosition(output, last_mouse_position);
SetConsoleActiveScreenBuffer(output);
CloseHandle(tmpbuf);
delete lcol;
}
//No pointers
dptr = &(bridge->DATA[x][y]);
001B41C6 mov eax,dword ptr [this]
001B41C9 mov ecx,dword ptr [eax+14h]
001B41CC movsx edx,word ptr [x]
001B41D0 imul edx,edx,370h
001B41D6 lea eax,[ecx+edx+1D4h]
001B41DD movsx ecx,word ptr [y]
001B41E1 shl ecx,4
001B41E4 add eax,ecx
001B41E6 mov dword ptr [dptr],eax
//With pointers
//Pointing to DATA[x]
012C41A5 mov eax,dword ptr [this]
012C41A8 mov ecx,dword ptr [eax+14h]
012C41AB movsx edx,word ptr [x]
012C41AF imul edx,edx,370h
012C41B5 lea eax,[ecx+edx+1D4h]
012C41BC mov dword ptr [y_dptr],eax
//Pointing to DATA[x]+y
012C41E0 movsx eax,word ptr [y]
012C41E4 shl eax,4
012C41E7 add eax,dword ptr [y_dptr]
012C41EA mov dword ptr [dptr],eax