散分给大家拜晚年。问题超级简单,快来快来~~~
使用如下程序测试对数组元素的访问速度:
#include <stdio.h>
#include <windows.h>
int main(int argc, char* argv[])
{
unsigned long t1,t2,t3;
int a[65536]={0},l,k = 0;
do{
t1 = GetTickCount();
for(int i = 0 ; i < 65535; i ++)
for(int j = 0 ; j < 65535 ; j ++)
l = a[1];
t2 = GetTickCount();
for(i = 0 ; i < 65535; i ++)
for(int j = 0 ; j < 65535 ; j ++)
l = a[65535];
t3 = GetTickCount();
printf("Access a[1] time = %ld\n",t2-t1);
printf("Access a[65535] time = %ld\n\n",t3-t2);
}while(++k < 100);
return 0;
}
在VC++ 6上的结果是:访问a[1]比访问a[65535]的速度快3-4倍。在VC++.NET上对两者的访问速度没有明显差别。那么,为什么会这样呢?为什么访问速度会有差异? 在VC++ 6调试模式下打开反汇编窗口, 看到l = a[1]和l=a[65535]的代码差不多,都是:
mov edx,dword ptr [ebp-xxxxh] // 两者仅仅是这里的xxxx不同
mov dword ptr [ebp-40010h],edx
奇怪!