Created
October 6, 2012 17:41
-
-
Save tobin/3845568 to your computer and use it in GitHub Desktop.
cin / scanf comparison
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
Comparison between cin and scanf | |
Usage: | |
make | |
./rand > rand.txt | |
Ctrl-C after a few seconds | |
wc rand.txt | |
time ./xor-c < rand.txt | |
time ./xor-cpp < rand.txt |
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
all: xor-c xor-cpp xor-cpp-noflush rand | |
test: xor-c xor-cpp xor-cpp-noflush | |
time ./xor-c < rand.txt | |
time ./xor-cpp < rand.txt | |
time ./xor-cpp-noflush < rand.txt | |
rand: rand.c | |
gcc -Wall rand.c -o rand | |
xor-c: xor.c | |
gcc -Wall -O4 xor.c -o xor-c | |
xor-cpp: xor.cc | |
g++ -Wall -O4 xor.cc -o xor-cpp | |
xor-cpp-noflush: xor-noflush.cc | |
g++ -Wall -O4 xor-noflush.cc -o xor-cpp-noflush |
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 <time.h> | |
#include <stdlib.h> | |
int main(int argc, char **argv) { | |
srand(time(NULL)); | |
while (1) | |
printf("%d\n", rand()); | |
} |
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 <iostream> | |
int main(int argc, char **argv) { | |
int parity = 0; | |
int x; | |
std::ios::sync_with_stdio(false); | |
while (std::cin >> x) | |
parity ^= x; | |
std::cout << parity << std::endl; | |
return 0; | |
} |
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> | |
int main(int argc, char **argv) { | |
int parity = 0; | |
int x; | |
while (1 == scanf("%d", &x)) | |
parity ^= x; | |
printf("%d\n", parity); | |
return 0; | |
} |
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 <iostream> | |
int main(int argc, char **argv) { | |
int parity = 0; | |
int x; | |
while (std::cin >> x) | |
parity ^= x; | |
std::cout << parity << std::endl; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment