# 值交换的两种方法及其效率分析

int c;

c = a;

a = b;

b = c;

a ^= b;

b ^= a;

a ^= b;

c = b;

mov         eax,dword ptr [ebp-8]

mov         dword ptr [ebp-0Ch],eax

b = a;

mov         ecx,dword ptr [ebp-4]

mov         dword ptr [ebp-8],ecx

a = c;

mov         edx,dword ptr [ebp-0Ch]

mov         dword ptr [ebp-4],edx

a ^= b;

mov         eax,dword ptr [ebp-4]

xor         eax,dword ptr [ebp-8]

mov         dword ptr [ebp-4],eax

b ^= a;

mov         ecx,dword ptr [ebp-8]

xor         ecx,dword ptr [ebp-4]

mov         dword ptr [ebp-8],ecx

a ^= b;

mov         edx,dword ptr [ebp-4]

xor         edx,dword ptr [ebp-8]

mov         dword ptr [ebp-4],edx

int a = 9;

int b = 8;

int c;

int nCount;

nCount = GetTickCount();

printf("Start time: %d/n", nCount);

for (int i = 0; i < 100000000; i++)

{

c = a;

a = b;

b = c;

}

nCount = GetTickCount();

printf("End   time: %d/n", nCount);

Start time: 38830024

End   time: 38830395

int a = 9;

int b = 8;

int nCount;

nCount = GetTickCount();

printf("Start time: %d/n", nCount);

for (int i = 0; i < 100000000; i++)

{

a ^= b;

b ^= a;

a ^= b;

}

nCount = GetTickCount();

printf("End   time: %d/n", nCount);

Start time: 38955024

End   time: 38956125

• 本文已收录于以下专栏：

举报原因： 您举报文章：值交换的两种方法及其效率分析 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)