2D数组中指向元素的指针减慢了代码的速度
Pointer to element in an 2D array slows down code
我有这段代码访问关于'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_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;
}
切入正题!
好吧!所以最近我有一个想法,像这样访问数组会减慢我的代码。据我所知,当你访问数组中的一个元素时,处理器将获取数组的基址,并从那里通过将元素的大小乘以用于查找指定元素地址的索引。我的想法是,如果我让处理器多次这样做,而不是仅仅创建一个指向地址的指针,然后用它来处理元素,那么它会减慢我的代码速度。所以我将代码重写如下:
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;
}
这个想法对我来说似乎很好,但问题是第一段代码比第二段代码快!所以我的问题是:为什么第一段代码比第二段代码快?
对于那些不喜欢阅读的人:
为什么第一段代码比另一段快?第一个用时0.0919秒,第二个用时0.226秒。
这也是程序集如何处理指针的副本:
//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
除了这部分代码之外,其余部分是相同的。
只看程序集,我们看到了一个额外的mov (y_dptr的赋值)。
看看这是如何在(外部)循环的每次迭代中完成的,并且代码中没有其他差异,这可能是性能下降的原因。除此之外,您的代码中实际上没有任何东西可以利用您试图使用的指针魔法。
使用dptr = (y_dptr+y);通过直接在指针上使用自增(y_dptr++;),可以丢失DPTR或y_dptr。
这是一些你没有使用的指针算术魔法,可以改进
相关文章:
- 使用"静态"如何影响我的代码速度?
- 加快一个长python代码的速度,因为只有一个块,所以速度很慢
- 将浮点数转换为字符串时如何加快此代码的速度?
- 为什么 pthread 会减慢代码速度?
- cudaMemcpyToSymbol只是挂起,永远不会返回.GPU 处理速度为 100%.代码在 K40 上工作正常,但
- 使用额外的变量使计算更容易理解 - (多少)我减慢了代码的速度?
- 与Java相比,为什么此C 代码执行速度如此慢
- 通过创建单个线程来运行一段代码可加快执行速度
- 代码执行/CPU 速度每 2 秒减慢一次
- 为什么下面的代码片段会加快代码速度
- 为什么这段代码在 C++ 中搜索子字符串的速度比在 C# 中慢得多
- 因为一行不相关的代码,速度相差那么大
- 添加print语句可以将代码的速度提高一个数量级
- 大括号的作用域是否用于注释目的,从而降低C++代码的速度
- 矩阵乘法代码使用avx2速度较慢
- 如果我用代码块编译代码,为什么我的代码运行速度更快
- 跟踪gcc编译以及哪些代码会减慢编译速度
- 可能的代码速度优化
- 如何在VS 2008中提高代码速度
- Java的JITter如何影响测量代码速度?