Skip to content

Instantly share code, notes, and snippets.

@matklad
Last active September 28, 2016 15:06
Show Gist options
  • Save matklad/764a1a7200217fd69739bbb7505e9ab4 to your computer and use it in GitHub Desktop.
Save matklad/764a1a7200217fd69739bbb7505e9ab4 to your computer and use it in GitHub Desktop.
Random C++ io benchmark
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)))
#include <iostream>
int main() {
unsigned parity = 0;
unsigned x;
while (std::cin >> x) {
parity ^= x;
}
std::cout << parity << std::endl;
}
#include <cstdio>
int main() {
unsigned parity = 0;
unsigned x;
while (1 == std::scanf("%u", &x)) {
parity ^= x;
}
std::printf("%u\n", parity);
}
#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;
}
λ ~/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
@matklad
Copy link
Author

matklad commented Sep 28, 2016

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment