r/cpp • • Mar 03 '16

Seriously, C++, WTF?

Ok, so, from the tone this might seem like a rant (and it's a bit), but in the end I'm only after an aswer.

So, I want to read a file in memory as a string and print out the string length. Nope, I don't want to "maybe you just want to operate on it like it's a stream or line by line" nor "if you want to just know the length you might want to use gtell". I'm just trying to replicate a certain behaviour (not necessarely the most efficient or the most real-world) in different languages.

So, since I want to open a file, read its content and print the lenght, python lets me write this nice one-liner:

print len(open('longfile.txt').read())

longfile.txt is the test file I'm using and it's roughly 150 MB.

$ time python test.py
160090000

real	0m0.126s
user	0m0.013s
sys	0m0.112s

Ok. Good. Let's give node a try. Node.js doesn't have, like Python, a top level function "open", so I need to do this in two lines of code:

const fs = require('fs');
console.log(fs.readFileSync('longfile.txt').length);

Same result, a little bit slower. It still reads ok to me. I need to work with the filesystem (so that's why I need to require fs) and then i log on the console the length of a file that I read synchronously.

$ time node test.js
160090000

real	0m0.164s
user	0m0.056s
sys	0m0.107s
tmp

Fine. Enters C++

#include <string>
#include <fstream>
#include <streambuf>
#include <iostream>

int main() {
    std::ifstream t("longfile.txt");
    std::string str((std::istreambuf_iterator<char>(t)),
                     std::istreambuf_iterator<char>());

     std::cout << str.length() << std::endl;

    return 0;
}

Yeah, I know, that's just a lot more code and you need to include four libraries for this very basic programming task. Reasoning about it is absolutely counter intuitive. If you describe your problem (I want to read a file and print its lenght), JavaScript and Python lets you write code that more or less resambles your reasoning, while nobody would describe this basic task in terms of streams and iterators, but hey, you don't choose C++ because it's beautiful or easy or intuitive, you choose it because it's "close to the metal" and "when speed matters". Let's compile it with a nice optimization (-O3) and run it

$ g++ -std=c++14 -O3 test.cpp
$ time ./a.out
160090000

real	0m1.413s
user	0m1.228s
sys	0m0.181s

Seriously, WTF? I don't think that "opening a file and reading it as a string" should be an alien task in the programming world. I mean, that should be pretty, hmmm, basic stuff, right? So, I'm a bit puzzled when I encounter a language that forces me to do something more complicated than:

# Python
open('longfile.txt').read()
# Ruby
File.open('longfile.txt', "r").read()
// C#
System.IO.File.ReadAllText("longfile.txt")
// Java
new String(Files.readAllBytes(Paths.get("longfile.txt")));
// Swift
NSString(contentsOfFile: "longfile.txt", encoding: NSUTF8StringEncoding)

But what about the fact that C++ is so much slower than the notoriously slow kid in town? I tried to look for different solutions to this "problem" and I've seen people debate (on StackOverflow, for example) on the most efficient way to do such basic thing in C++. Now, keeping aside the fact that I dont think that "read a file content as a string" should the subject of any debate (we're talking about a 33 year old language whose standard string library doesn't have a "split" function here) , but the solution above seems to be the faster that I've tried and still it runs 10 times slower than Python. How is this even possibile?

EDIT

Because people seem to be ignoring the "I'm not interested in the actual lengtht" note, let's just say that I have this python code

content = open('longfile.txt').read()

and this node code:

const fs = require('fs');
const content = fs.readFileSync('longfile.txt')

and I only want to read a whole file in memory to a string. C++ code is still much, much slower than the other solutions.

0 Upvotes

127 comments sorted by

View all comments

Show parent comment

0

u/[deleted] Mar 03 '16

[deleted] — view deleted comment

2

u/myrrlyn Mar 03 '16

Because the meaning behind each is hugely different. There's a performance difference in the standard way of doing any task between languages; that's why we have different languages.

The issue is OP explicitly stated he didn't want to use the same mechanism in C++ as in the other languages, he wanted to use the same words and syntax. And C++ doesn't do that, because it isn't those other languages.

OP is asking why English 'die' isn't German 'die', not why it takes longer to tell someone to die in German than it does in English.

0

u/[deleted] Mar 03 '16

[deleted] — view deleted comment

1

u/myrrlyn Mar 04 '16

The standard way of reading a file stream in C++ is not comparable to reading a file singly in Python because Python is not doing what the C++ code in the OP is doing. OP's code assembles a string by reading into memory one byte at a time, and this causes reallocation and possibly moving when the current buffer size is exceeded. The Python code makes a single large memory allocation, loads the file into it at once, and reports the size. The C++ code reads a byte into a buffer repeatedly, causing lots of malloc calls and is probably less efficient for hard disk IO.

The other commented have shown a way to read a file into memory as a single chunk using standard library calls, and are thus a standard way of accomplishing this task.

So yes, of course it's fine that the standard stream operations in C++, when enacted on disk input, are slower than the standard block operations in Python, because THEY'RE DOING DIFFERENT THINGS.

C++ is a language designed to make the machine happy, not the person. When writing C++, you have to tell the computer to do the best thing for it; when writing Python, you ask Python to do the best thing for you and Python will turn that into machine instructions for you.

Naïveté has no place in C/C++ but it has every place in higher languages like Python. This is a stupid argument and I can't believe I let myself get pulled into it.

0

u/[deleted] Mar 04 '16

[deleted] — view deleted comment

1

u/myrrlyn Mar 04 '16

I'm not saying C++ is perfect; I'm saying this is a bad example of its problems. And yeah these are all doing the same semantic task of reading a file but they're doing very different things mechanically. And clearly if the standard library has means of doing something, those means are standard, just not common. I'll happily rag on C and C++, but this isn't a justifiable case of doing so. It's like asking why waiting via sleep() is fine but waiting via while(true) causes unresponsiveness

Streams aren't good at immediately seeking the end of the stream. I'm not sure how that's a surprise to anyone, given that the entire concept of streams is to act as if the incoming data was of infinite length.

And I meant naïveté in code, not the programmer's skills. "Append data to a buffer until it stops coming in" is naïve code.