Why is reading lines from stdin much slower in C++ than Python?
stackoverflow.com
stackoverflow.com
With some experimenting, I discovered that even Perl was much faster than C++ with getline(). (Note that this was input of a file, not stdin as in this article.)
I've not used getline() since.
In any case, the C++ equivalent to fgets is sgetn on the underlying streambuf or filebuf - http://en.cppreference.com/w/cpp/io/basic_streambuf/sgetn . I'd use this before I used fgets, if for no other reason than I then don't have to remember to call fclose()
The fact that you haven't used one of the most trivial functions in the standard library because of a slight performance penalty 15 years ago is worrying.
My hunch would be that you was discarding the std::string between lines (thereby not reusing the buffer and causing additional memory allocation for each line)... but I'm not sure even that would cause a 12 fold slowdown.
Its worth noting that even Cs i/o APIs are typically synchronized across threads so you can output lines to stdout without experiencing the same issue within just vanilla threaded C code.
std::ios_base::sync_with_stdio(false);Nobody ever claimed that C++ didn't require more knowledge to use effectively.
http://www.gnu.org/software/libc/manual/html_node/Buffering-...
http://www.gnu.org/software/libc/manual/html_node/Controllin...
there are also threading issues:
http://www.gnu.org/software/libc/manual/html_node/Streams-an...
glibc even lets you implement custom streams (FILE* handles that will work with fgets etc. but call your own source and sink to do i/o)
Like they tell you in school, premature optimization is the root of all evil. So don't worry about this until you need it.
Isn't the Python interpreter written in C?
I've never found it to be so, and it's not a common complaint.
Would you care to elaborate? I typed "man straightforward" into terminal and nothing was thrown back at me.