level 9
#include <stdio.h>
#include<conio.h>
#include <time.h>
#define N 10
int globle=4;
int func1(void)
{int temp=globle;return temp;}
int func2(int t)
{int temp=t;return temp;}
int main(){
clock_t start,end,evarage;
int local=4;
int i,j,k;
start=clock();
for(j=0;j<N;j++)
for(i=0;i<100000;i++)
{func1();}
end=clock();
printf("globle variable %d %d %d\n",start,end,end-start);
start=clock();
for(j=0;j<N;j++)
for(i=0;i<100000;i++)
{func2(globle);}
end=clock();
printf("local variable %d %d %d\n",start,end,end-start);
getch();
return 0;
}
2013年04月13日 02点04分
9
level 12
全局变量和局部变量哪个快,可以直接看汇编代码。我先把程序简化一下:
#include<conio.h>
int globle=4;
int func1(void)
{
int temp=globle;
return temp;
}
int func2(int t)
{
int temp=t;
return temp;
}
int main()
{
func1();
func2(globle);
return 0;
}
2013年04月15日 07点04分
10
level 12
然后在 vc6 里面编译,按 F10 开始调试,按 Alt + 8 打开汇编窗口。然后就可以找到对应的代码了。
2013年04月15日 07点04分
11
level 12
先看调用的对比:
func1();
004010C8 call @ILT+10(func1) (0040100f)
func2(globle);
004010CD mov eax,[globle (00424a30)]
004010D2 push eax
004010D3 call @ILT+0(func2) (00401005)
004010D8 add esp,4
2013年04月15日 07点04分
12
level 12
再看看函数体的代码:
6: int func1(void)
7: {
00401030 ……
8: int temp=globle;
00401048 mov eax,[globle (00424a30)]
0040104D mov dword ptr [ebp-4],eax
9: return temp;
00401050 mov eax,dword ptr [ebp-4]
10: }
12: int func2(int t)
13: {
00401070 ……
14: int temp=t;
00401088 mov eax,dword ptr [ebp+8]
0040108B mov dword ptr [ebp-4],eax
15: return temp;
0040108E mov eax,dword ptr [ebp-4]
16: }
2013年04月15日 07点04分
13