Skip to content

Instantly share code, notes, and snippets.

@milleniumbug
Last active June 1, 2018 14:49
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 milleniumbug/d2d0d3ecfd96c5c36a2d7d92b464ab47 to your computer and use it in GitHub Desktop.
Save milleniumbug/d2d0d3ecfd96c5c36a2d7d92b464ab47 to your computer and use it in GitHub Desktop.
#include <random>
#include <iostream>
int main()
{
std::random_device rd;
std::uniform_int_distribution<> dist(-1000000, 1000000);
for(int i = 0; i < 10000000; ++i)
std::cout << dist(rd) << "\n";
}
#include <cstdio>
#include <chrono>
#include <iostream>
int main() {
using std::chrono::steady_clock;
using std::chrono::duration_cast;
using std::chrono::milliseconds;
auto start = steady_clock::now();
int i;
for (auto _ = 0; _ < 10'000'000; ++_) {
std::cin >> i;
}
auto end = steady_clock::now();
std::cout << "cin: " << duration_cast<milliseconds>(end - start).count() << '\n';
}
#include <cstdio>
#include <chrono>
#include <iostream>
int main() {
using std::chrono::steady_clock;
using std::chrono::duration_cast;
using std::chrono::milliseconds;
auto start = steady_clock::now();
int i;
for (auto _ = 0; _ < 10'000'000; ++_) {
scanf("%d", &i);
}
auto end = steady_clock::now();
std::cout << "scanf: " << duration_cast<milliseconds>(end - start).count() << '\n';
}
➜ ~ g++ -v
Using built-in specs.
COLLECT_GCC=g++
COLLECT_LTO_WRAPPER=/usr/libexec/gcc/x86_64-redhat-linux/6.3.1/lto-wrapper
Target: x86_64-redhat-linux
Configured with: ../configure --enable-bootstrap --enable-languages=c,c++,objc,obj-c++,fortran,ada,go,lto --prefix=/usr --mandir=/usr/share/man --infodir=/usr/share/info --with-bugurl=http://bugzilla.redhat.com/bugzilla --enable-shared --enable-threads=posix --enable-checking=release --enable-multilib --with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions --enable-gnu-unique-object --enable-linker-build-id --with-linker-hash-style=gnu --enable-plugin --enable-initfini-array --disable-libgcj --with-isl --enable-libmpx --enable-gnu-indirect-function --with-tune=generic --with-arch_32=i686 --build=x86_64-redhat-linux
Thread model: posix
gcc version 6.3.1 20161221 (Red Hat 6.3.1-1) (GCC)
➜ ~ g++ -O2 -std=c++14 cin.cpp -Wall -Wextra -pedantic -o cin.o
➜ ~ g++ -O2 -std=c++14 scanf.cpp -Wall -Wextra -pedantic -o scanf.o
➜ ~ for i in `seq 1 10`; do ./cin.o < nums; ./scanf.o < nums; done
cin: 4040
scanf: 1575
cin: 4293
scanf: 1561
cin: 4037
scanf: 1567
cin: 4078
scanf: 1528
cin: 4012
scanf: 1575
cin: 4074
scanf: 1552
cin: 4038
scanf: 1534
cin: 4044
scanf: 1535
cin: 4118
scanf: 1533
cin: 4002
scanf: 1548
(with std::cin.sync_with_stdio(false); )
➜ ~ for i in `seq 1 10`; do ./cin.o < nums; ./scanf.o < nums; done
cin: 1435
scanf: 1586
cin: 1412
scanf: 1526
cin: 1457
scanf: 1574
cin: 1433
scanf: 1527
cin: 1439
scanf: 1591
cin: 1464
scanf: 1586
cin: 1434
scanf: 1571
cin: 1401
scanf: 1610
cin: 1435
scanf: 1567
cin: 1414
scanf: 1604
(with std::cin.sync_with_stdio(false); and std::cin.tie(nullptr); )
cin: 1367
scanf: 1584
cin: 1329
scanf: 1551
cin: 1329
scanf: 1570
cin: 1340
scanf: 1587
cin: 1315
scanf: 1585
cin: 1382
scanf: 1539
cin: 1386
scanf: 1624
cin: 1343
scanf: 1551
cin: 1354
scanf: 1587
cin: 1314
scanf: 1608
I lost the actual run times, but the overall results were something along:
MSVC has scanf being 3 times faster than cin with unmodified code
and 2 and 1/3 times faster than cin with std::cin.sync_with_stdio(false); and std::cin.tie(nullptr);
(release mode, x64)
MinGW has scanf being 13/9 times faster than cin with unmodified code
MinGW has scanf being 18/5 times slower than cin with std::cin.sync_with_stdio(false); and std::cin.tie(nullptr);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment