
Why is reading lines from stdin much slower in C++ than Python? - Arkid
http://stackoverflow.com/questions/9371238/why-is-reading-lines-from-stdin-much-slower-in-c-than-python
======
jemfinch
Whenever this question is asked, it's almost _always_ std::sync_with_stdio.

~~~
16s
Yes, and it's an easy fix as the accepted answer shows:

cin.sync_with_stdio(false);

------
jjguy
Reminds me of Joel's Law of Leaky Abstractions. At some point, you must deeply
understand the entire stack.

[http://www.joelonsoftware.com/articles/LeakyAbstractions.htm...](http://www.joelonsoftware.com/articles/LeakyAbstractions.html)

------
spullara
When I/O is slow the first thing to check is to make sure it is buffered. This
is true in every language and library. Just turns out this guy ran into
something with a default that matches the "read input from terminal" case
instead of the "process millions of lines" case.

------
aeurielesn
The almighty cin has it disadvantages when it comes down to performance.
That's why I will always prefer scanf/gets, for a rather simple I/O
performance boost.

~~~
rwos
Using gets() also makes writing exploits much quicker - win-win! ;-)

Sorry, I couldn't resist - you are of course right with the general stdio over
std::iostream thing, though. I've also found that memory usage and executable
size explode when using streams - though that's not C++'s fault per se, more a
stdlib/compiler problem.

------
ExpiredLink
Real programmers don't use iostream. Seriously.

~~~
joelthelion
Real programmers don't waste time making swooping generalizations about other
programmers.

~~~
ExpiredLink
To be more precise: Real world C++ programmers don't use iostreams as I know
from personal experience (more than 10 years of C++ programming).

Edit: Several (more or less successful) attempts have been made for an
alternative library. <http://accu.org/index.php/journals/1539> for an
overview.

~~~
X-Istence
In a lot of cases, so far in my career, using iostream's and sstreams and
fstreams in C++ is absolutely fine because most of the programs time isn't
even spent there, it is spent in the data crunching algorithms.

I am a real world C++ programmer, I can only claim about 5 years experience
though, and I use iostream. So yes, your data is anecdotal, so is mine, and we
are both complete opposites.

------
serialx
It shows us why just using C/C++ is not a performance-wise decision. You need
to spend more time developing the product and spend MORE time to improve it's
performance.

~~~
mhurron
I think it more shows that just picking C/C++ over Python doesn't mean you
automatically get awesome performance. You still need to know what your doing.

~~~
adobriyan
like using `gettimeofday()` and not using cat(1), for starters.

~~~
jjc4p
What's bad about cat(1)?

~~~
shabble
Nothing, except it's entirely unnecessary for this task.

See "Useless use of cat awards" from days of yore:
<http://partmaps.org/era/unix/award.html#uucaletter>

~~~
truncate
I still prefer cat, as one simple mistake of < to > and the file is gone.

