Created
January 12, 2013 01:12
-
-
Save biwakonbu/4515466 to your computer and use it in GitHub Desktop.
XOR 交換と一時変数を使った swap の速度比較用プログラム。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <string.h> | |
#define TRUE 1 | |
#define FALSE 0 | |
#define XOR_CHANGE(a,b) { \ | |
if(a != b) { \ | |
a ^= b; \ | |
b ^= a; \ | |
a ^= b; \ | |
} \ | |
} | |
#define SWAP(a,b) { \ | |
int temp; \ | |
temp = a; \ | |
a = b; \ | |
b = temp; \ | |
} | |
int main(int argc, char *argv[]) | |
{ | |
int i; | |
int x=1, y=2; | |
int xor_change_flg = FALSE; | |
if (--argc > 0) { | |
if (strcmp(*++argv, "--xor-change") == 0) { | |
xor_change_flg = TRUE; | |
} | |
} | |
for(i = 0; i < 1000000000; i++) { | |
if(xor_change_flg) { | |
XOR_CHANGE(x, y); | |
} else { | |
SWAP(x, y); | |
} | |
} | |
printf(xor_change_flg? "XOR_CHANGE" : "SWAP: "); | |
printf("x: %d, y: %d\n", x, y); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment