News for hackers. Stuff that matters.
Re:Why is reading lines from stdin much slower in C++ than P... by jemfinch on Saturday March 10, 2012 @10:48PM (#3689480)
Whenever this question is asked, it's almost always std::sync_with_stdio.
Re:Why is reading lines from stdin much slower in C++ than P... by 16s on Sunday March 11, 2012 @09:26AM (#3690315)
Yes, and it's an easy fix as the accepted answer shows:

cin.sync_with_stdio(false);

Re:Why is reading lines from stdin much slower in C++ than P... by codemac on Sunday March 11, 2012 @06:18AM (#3690076)
I think it's a larger problem that defaults in standard libraries are rarely understood. For python OR for C++.

I'm asking to the larger Hacker News community here, how do you illustrate the dependencies of a standard library API call without making it more complicated? Syntax is nothing, understanding is everything.

Re:Why is reading lines from stdin much slower in C++ than P... by jjguy on Sunday March 11, 2012 @01:07AM (#3689712)
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...

Re:Why is reading lines from stdin much slower in C++ than P... by spullara on Sunday March 11, 2012 @03:11PM (#3690987)
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.
Re:Why is reading lines from stdin much slower in C++ than P... by aeurielesn on Saturday March 10, 2012 @11:32PM (#3689548)
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.
Re:Why is reading lines from stdin much slower in C++ than P... by rwos on Sunday March 11, 2012 @07:50AM (#3690202)
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.

Re:Why is reading lines from stdin much slower in C++ than P... by ExpiredLink on Sunday March 11, 2012 @06:56AM (#3690125)
Real programmers don't use iostream. Seriously.
Re:Why is reading lines from stdin much slower in C++ than P... by joelthelion on Sunday March 11, 2012 @08:08AM (#3690218)
Real programmers don't waste time making swooping generalizations about other programmers.
Re:Why is reading lines from stdin much slower in C++ than P... by ExpiredLink on Sunday March 11, 2012 @08:17AM (#3690234)
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.

Re:Why is reading lines from stdin much slower in C++ than P... by X-Istence on Sunday March 11, 2012 @10:29AM (#3690394)
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.

Re:Why is reading lines from stdin much slower in C++ than P... by alexeiz on Sunday March 11, 2012 @05:50PM (#3691394)
Raw number of years of experience doesn't mean much. I've seen developers who started using C++ in the early nineties, but haven't bothered to update their knowledge of C++ since then. If I have more years of experience than you, does it make my opinion more authoritative than yours?
Re:Why is reading lines from stdin much slower in C++ than P... by alexeiz on Sunday March 11, 2012 @05:51PM (#3691399)
Indeed they don't. There are no iostreams in FORTRAN.
Re:Why is reading lines from stdin much slower in C++ than P... by serialx on Saturday March 10, 2012 @10:19PM (#3689437)
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.
Re:Why is reading lines from stdin much slower in C++ than P... by mhurron on Saturday March 10, 2012 @10:39PM (#3689467)
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.
Re:Why is reading lines from stdin much slower in C++ than P... by adobriyan on Sunday March 11, 2012 @03:47AM (#3689883)
like using `gettimeofday()` and not using cat(1), for starters.
Re:Why is reading lines from stdin much slower in C++ than P... by jjc4p on Sunday March 11, 2012 @09:10AM (#3690293)
What's bad about cat(1)?
Re:Why is reading lines from stdin much slower in C++ than P... by shabble on Sunday March 11, 2012 @11:24AM (#3690491)
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

Re:Why is reading lines from stdin much slower in C++ than P... by truncate on Sunday March 11, 2012 @04:08PM (#3691140)
I still prefer cat, as one simple mistake of < to > and the file is gone.
Re:Why is reading lines from stdin much slower in C++ than P... by adobriyan on Sunday March 11, 2012 @07:21PM (#3691595)
OP intends to measure time to read data from file and maybe it process into internal representation.

cat(1) almost certainly internally buffers data (32KB here) thus context switches occurs. Shell creates pipe which is buffered inside kernel.

All of this muffles measurements.

What was one or several read(2) calls + processing is now one or several calls of maybe smaller sizes + whatever scheduling differences + in one of the examples OP used /usr/bin/time of the whole thing also.

This is of course is not visible because data were dumbed down by using time() which has horrible granularity, but when finer grained timer it'd be visible, I'm sure.

Re:Why is reading lines from stdin much slower in C++ than P... by pyrotechnick on Saturday March 10, 2012 @11:33PM (#3689552)
you're
Re:Why is reading lines from stdin much slower in C++ than P... by meastham on Saturday March 10, 2012 @10:23PM (#3689444)
That's quite a lot to read into a tiny example like this.
Re:Why is reading lines from stdin much slower in C++ than P... by _delirium on Saturday March 10, 2012 @10:43PM (#3689472)
I agree, though I think it could fairly be taken as one small bit of evidence in favor of "C++ has a lot of gotchas". In this case it looks like the culprit is C++'s C-compatibility-driven decision to sync with stdio by default, and therefore to avoid buffering input. Of course, if they made the opposite decision on defaults, "C++ doesn't sync with stdio by default" would be a different, probably also common, variety of "gotcha".
Re:Why is reading lines from stdin much slower in C++ than P... by mikeash on Sunday March 11, 2012 @07:09AM (#3690151)
Why isn't cin implemented on top of C's stdin and FILE? That way you get both buffering and compatibility.
Re:Why is reading lines from stdin much slower in C++ than P... by jemfinch on Sunday March 11, 2012 @11:44AM (#3690526)
I've not implemented the C++ std library, but my guess is it's because iostreams need to implement their own buffering anyway, so it would just add complexity and unpredictability to buffer atop an already-buffering library.
Re:Why is reading lines from stdin much slower in C++ than P... by mikeash on Sunday March 11, 2012 @09:51PM (#3692005)
I'm not sure this quite makes sense. The buffering can already be disabled, clearly, since that's what's being discussed. The non-buffering implementation could be easily placed atop FILE (I don't know the details, but I can't imagine a FILE-based iostream implementation being at all complex) at which point you have a buffered implementation that also cooperates with pure C stdio. iostream would need buffering for other operations, but could just leave it off permanently for stdio, and the switch already exists.