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

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;<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

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

#### java交换两个数的常见方法及效率测试

2015-08-05 11:31:03

#### C/C++用多种方法交换两个数a和b的值

2017-10-31 11:06:50

#### 交换两个变量值的三种方法（Java实现）

2016-03-27 16:44:42

#### 交换两个数的值的三种指针方法——指针的引用和指向指针的指针

2014-05-17 14:29:29

#### C语言三种方法实现两个变量值的交换

2014-03-25 00:28:09

#### 实现两个int 型数据交换的三种方法

2017-03-19 22:00:18

#### 排序算法及其效率分析（一）内排序

2015-04-30 21:33:31

#### Java算法——交换两个变量的值，3种方法

2016-08-08 22:33:46

#### 不借助第三方变量，交换两个整型数x和y的值

2012-06-17 13:58:39

#### JavaScript交换两个变量值的四种方法

2017-02-11 12:33:45