Skip to content

Instantly share code, notes, and snippets.

@suyash
Last active October 14, 2016 14:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save suyash/f9739e7f019f25634f4382cb25bc1fe6 to your computer and use it in GitHub Desktop.
Save suyash/f9739e7f019f25634f4382cb25bc1fe6 to your computer and use it in GitHub Desktop.
cin vs scanf

ubuntu 14.04, g++ 4.8

81.700000, 187.500000

ubuntu 14.04, clang++-3.8

76.400000, 186.800000
#include <chrono>
#include <functional>
#include <iostream>
typedef std::function<void()> Reader;
uint64_t bench(uint64_t iterations, const Reader& reader) {
auto start = std::chrono::system_clock::now();
for (uint64_t i = 0; i < iterations; i++) {
reader();
}
auto end = std::chrono::system_clock::now();
return std::chrono::duration_cast<std::chrono::milliseconds>(end -
start)
.count();
}
int main() {
uint64_t iter = 250000;
uint64_t runs = 10;
double t_scan = 0, t_cin = 0;
for (uint64_t x = 0; x < runs; x++) {
t_scan += bench(iter, []() {
int64_t a = 0, b = 0, c = 0;
scanf("%ld %ld %ld", &a, &b, &c);
});
t_cin += bench(iter, []() {
int64_t a = 0, b = 0, c = 0;
std::cin >> a >> b >> c;
});
}
printf("%lf, %lf\n", t_scan / runs, t_cin / runs);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment