Skip to content

Instantly share code, notes, and snippets.

@tobin
Created October 6, 2012 17:41
Show Gist options
  • Star 9 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save tobin/3845568 to your computer and use it in GitHub Desktop.
Save tobin/3845568 to your computer and use it in GitHub Desktop.
cin / scanf comparison
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
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
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
int main(int argc, char **argv) {
srand(time(NULL));
while (1)
printf("%d\n", rand());
}
#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;
}
#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;
}
#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