#include <iostream>
#include <sys/time.h>	// gettimeofday()
#include <climits>		// UINT_MAX

using namespace std;

#define NUMSTEPS UINT_MAX

int main(){
	timeval t1, t2, t3, t4;
	unsigned int a = 32, b = 64, c = 0, i = 0;
	double elapsedTime;

	gettimeofday(&t1, NULL);
	for(i = 0; i < NUMSTEPS; ++i){
		swap(a, b);
	}

	gettimeofday(&t2, NULL);
	for(i = 0; i < NUMSTEPS; ++i){
		a^=b;
		b^=a;
		a^=b;
	}

	gettimeofday(&t3, NULL);
	for(i = 0; i < NUMSTEPS; ++i){
		c = b;
		b = a;
		a = c;
	}
	gettimeofday(&t4, NULL);

	cout<<"NUMSTEPS = "<<NUMSTEPS<<endl;
	// compute and print the elapsed time in millisec
	elapsedTime = (t2.tv_sec - t1.tv_sec) * 1000.0;      // sec to ms
	elapsedTime += (t2.tv_usec - t1.tv_usec) / 1000.0;   // us to ms
	cout << "swap\t" << elapsedTime << " ms.\n";

	elapsedTime = (t3.tv_sec - t2.tv_sec) * 1000.0;      // sec to ms
	elapsedTime += (t3.tv_usec - t2.tv_usec) / 1000.0;   // us to ms
	cout << "XOR\t" << elapsedTime << " ms.\n";

	elapsedTime = (t4.tv_sec - t3.tv_sec) * 1000.0;      // sec to ms
	elapsedTime += (t4.tv_usec - t3.tv_usec) / 1000.0;   // us to ms
	cout << "+var\t" << elapsedTime << " ms.\n";
	cout<<a<<b<<c<<"\b\b\b\b\b\b";
	return 0;
}
