Last active
September 28, 2016 15:06
-
-
Save matklad/764a1a7200217fd69739bbb7505e9ab4 to your computer and use it in GitHub Desktop.
Random C++ io benchmark
This file contains hidden or 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
import random | |
numbers = (random.randrange(2**32) for _ in range(3 * 10**6)) | |
with open("in.txt", "w") as f: | |
f.write(" ".join(map(str, numbers))) |
This file contains hidden or 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() { | |
unsigned parity = 0; | |
unsigned x; | |
while (std::cin >> x) { | |
parity ^= x; | |
} | |
std::cout << parity << std::endl; | |
} |
This file contains hidden or 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 <cstdio> | |
int main() { | |
unsigned parity = 0; | |
unsigned x; | |
while (1 == std::scanf("%u", &x)) { | |
parity ^= x; | |
} | |
std::printf("%u\n", parity); | |
} |
This file contains hidden or 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() { | |
unsigned parity = 0; | |
unsigned x; | |
std::ios::sync_with_stdio(false); | |
while (std::cin >> x) { | |
parity ^= x; | |
} | |
std::cout << parity << std::endl; | |
} |
This file contains hidden or 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
λ ~/iobench/ python3 gen.py | |
λ ~/iobench/ clang++ -Wall -Werror -O2 iostream.cpp -o iostream | |
λ ~/iobench/ clang++ -Wall -Werror -O2 cstdio.cpp -o cstdio | |
λ ~/iobench/ clang++ -Wall -Werror -O2 iostream_unsynced.cpp -o iostream_unsynced | |
λ ~/iobench/ ./iostream < in.txt && ./cstdio < in.txt && ./iostream_unsynced < in.txt | |
3930846996 | |
3930846996 | |
3930846996 | |
λ ~/iobench/ time ./iostream < in.txt | |
3930846996 | |
./iostream < in.txt 1.44s user 0.01s system 99% cpu 1.447 total | |
λ ~/iobench/ time ./cstdio < in.txt | |
3930846996 | |
./cstdio < in.txt 0.48s user 0.01s system 99% cpu 0.492 total | |
λ ~/iobench/ time ./iostream_unsynced < in.txt | |
3930846996 | |
./iostream_unsynced < in.txt 0.34s user 0.00s system 99% cpu 0.351 total |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Credit: http://stackoverflow.com/a/12762166