swap1:Each pair of expressions are separated by comma.
swap2:Each pair of expressions are divided by semicolon.
swap3:inline function
#include<iostream>
#include<windows.h>
#define swap1(a,b) a^=b,b^=a,a^=b
#define swap2(a,b) a^=b;b^=a;a^=b;
using namespace std;
LARGE_INTEGER t1,t2,tc;
void begin()
{
QueryPerformanceFrequency(&tc);
QueryPerformanceCounter(&t1);
}
void end()
{
QueryPerformanceCounter(&t2);
cout<<1.0*(t2.QuadPart-t1.QuadPart)/tc.QuadPart<<endl;
}
inline void swap3(int &a,int &b)
{
int temp=a;
a=b;
b=temp;
}
int main()
{
int a=5,b=3;
begin();
for(int i=0;i<10000000;i++)
swap1(a,b);
cout<<"swap1: ";
end();
begin();
for(int i=0;i<10000000;i++)
swap2(a,b);
cout<<"swap2: ";
end();
begin();
for(int i=0;i<10000000;i++)
swap3(a,b);
cout<<"swap3: ";
end();
return 0;
}
Test result:
swap1: 0.0788353
swap2: 0.024506
swap3: 0.115301
--------------------------------
Process exited with return value 0
Press any key to continue . . .