
#include <stdlib.h>
#include <time.h>
#include <stdio.h>

#define TIMES 200000000

int main(){
    volatile int a;
    volatile int b;
    volatile int c;
    int i;
    clock_t t1 = clock();

    srand(time(NULL));
    a = rand();
    b = rand();

	// # on x86_64 gcc 4.8.1

    for( i = 0; i < TIMES; i++ ){
        c = a;
        // movl    -36(%rbp), %eax
		// movl    %eax, -44(%rbp)

        a = b;
	    // movl    -40(%rbp), %eax
	    // movl    %eax, -36(%rbp)

        b = c;
	    // movl    -44(%rbp), %eax
	    // movl    %eax, -40(%rbp)
    }

    clock_t t2 = clock();
    for( i = 0; i < TIMES; i++ ){
        a ^= b;
        // movl    -36(%rbp), %edx
        // movl    -40(%rbp), %eax
        // xorl    %edx, %eax
        // movl    %eax, -36(%rbp)
        b ^= a;
	    // movl    -40(%rbp), %edx
    	// movl    -36(%rbp), %eax
    	// xorl    %edx, %eax
    	// movl    %eax, -40(%rbp)
        a ^= b;
    	// movl    -36(%rbp), %edx
    	// movl    -40(%rbp), %eax
    	// xorl    %edx, %eax
    	// movl    %eax, -36(%rbp)
    }
    clock_t t3 = clock();
    double s_var = (double)(t2-t1)/CLOCKS_PER_SEC;
    double s_xor = (double)(t3-t2)/CLOCKS_PER_SEC;
    double s_all = (double)(t3-t1)/CLOCKS_PER_SEC;

    printf("swap by var: %.2fs\n",s_var);
    printf("swap by xor: %.2fs\n",s_xor);
    printf("ratio%% var: %d\n",(int)((s_var/s_all)*100));
    printf("ratio%% xor: %d\n",(int)((s_xor/s_all)*100));
    printf("xor/var: %.2f\n",(s_xor/s_var));
	return 0;
}