Live data from Hacker News

Popular Myths about C++, Part 3

isocpp.org

61–70 of 114 posts

Re: Popular Myths about C++, Part 3

#61
post #51

Bjarne should know better than to directly compare the performance of std::sort and qsort. One is typically printed in full in a header file, while the other is typically compiled separately. If qsort were found in a header file, it could be inlined into identical code to the C++ version, regardless of the fact that there are void pointers lying around everywhere. There are caveats: the compiler might not choose to i…

Are you sure that the callback can be inlined though? How would that work, the calls through the pointer would be replaced with the function body? I would assume that qsort itself could be, but that wouldn't help much.

Re: Popular Myths about C++, Part 3

#62

> I have never seen qsort beat sort Well, here you go: https://gist.github.com/ridiculousfish/bb511993deba1d148317 qsort: 674 ms std::sort: 1104 ms qsort only requires one invocation of the comparator to determine the order, while std::sort often requires two. So qsort ought to be faster when comparisons are expensive.

[deleted]

Re: Popular Myths about C++, Part 3

#63

> I have never seen qsort beat sort Well, here you go: https://gist.github.com/ridiculousfish/bb511993deba1d148317 qsort: 674 ms std::sort: 1104 ms qsort only requires one invocation of the comparator to determine the order, while std::sort often requires two. So qsort ought to be faster when comparisons are expensive.

C++14 version that sorts random strings: https://gist.github.com/det/57c7f0e377e02ccc696f

Re: Popular Myths about C++, Part 3

#64
post #55

Earlier quoted context omitted.

Catbirds like Linus Torvalds, for example?

Does Torvalds write anything in C++? The kernel and git are in C

This is my point exactly. I'd consider both git and the Linux kernel tough projects, so the choice of the programming language is interesting. More so, given his stance on C++.

Re: Popular Myths about C++, Part 3

#65
post #54
post #52

Earlier quoted context omitted.

There's an example in Part 1, where string concatenation is used as an example. C++ requires "adding" two string objects and C requires manipulating char pointers. And thus, C++ is a better teaching programming language. Although true, I feel this argument is rather weak: it's true, that when teaching I wouldn't want to start with pointers and malloc's from the get go, but it does not mean C++ is the only alternative…

If you're teaching programming, don't teach C++. If you're teaching systems programming, don't hide pointers. In this case, knowing the addition operator is useful iff they understand the underlying operations.... chances are, if they're learning C++, they don't.

I completely agree.

It's difficult to find a place for C++ in my little projects, given that I can use C for low level stuff and Python or Lua for high level stuff.

Re: Popular Myths about C++, Part 3

#66
post #50

> “To understand C++, you must first learn C” > “C++ is an Object-Oriented Language” > “For reliable software, you need Garbage Collection” > “For efficiency, you must write low-level code” > “C++ is for large, complicated, programs only” Well, 2.5/5 of those aren't myths. You certainly don't need to write low-level code for efficiency, C++ does a rather poor job of acting like an OO language, and you don't need Garb…

"And C++ isn't just for large, complicated programs; it's for small, complicated programs too." hahahahahahaha!!!!!!!!!!!!!!!!!!! -- I was thinking that, but hadn't put it into those exact words yet. I was thinking something like "C++ is definitely for complicated programs" -- not that they necessarily need to be complicated, but that C++ often unnecessarily complicates them. I really wanted to like the STL a long ti…

I want to explain why you are getting down-voted: Hacker News don't like onomatopoeias like “hahahahahahaha!!!!!!!!!!!!!!!!!!!”. In general, a neutral tone is preferred and tongue-in-cheek is acceptable (like parent).

This is an important difference with most subreddits (some, like /r/AskHistorian have a similar ambiance); it avoids ending up with long, heated and shallow conversations.

Re: Popular Myths about C++, Part 3

#67

> I have never seen qsort beat sort Well, here you go: https://gist.github.com/ridiculousfish/bb511993deba1d148317 qsort: 674 ms std::sort: 1104 ms qsort only requires one invocation of the comparator to determine the order, while std::sort often requires two. So qsort ought to be faster when comparisons are expensive.

Ran it about 10 times and using msvc the sort version is around 2.5 times faster. Maybe you just got lucky once? Or something went wrong with timing?

Re: Popular Myths about C++, Part 3

#68

One of the reasons people use "low level code" for performance is because the STL doesn't easily provide control of memory which is critical to performance. Electronic Arts wrote their own version of the STL largely so they could better control memory [1]. I'm not really sure about the rest of the myths. I'm a little confused about how "To understand C++, you must first learn C” is a myth since C++ is a superset of C…

While the C++ language is indeed a superset of the C language, the paradigms of modern C++ have almost no overlap with those of C. See for example the string qsort vs string std::sort elsewhere in this thread. The myth in "To understand C++, you must first learn C" is not that C and C++ are unrelated. The myth is that learning C helps you understand C++. The reality is that telling people to learn C first is an excel…

I'd argue that UB is an evil enough concept in C that still exists and is expanded on in C++ that should be known by every programmer. Sure, paradigm differences mean that idiomatic C code is not idiomatic C++ code, but knowing the pitfalls of the language should be mandatory, imho.

Re: Popular Myths about C++, Part 3

#69
I used a container version of sort() to avoid being explicit about the iterators

Is that something new in C++14, coulnd't immediately find it on the net? Or is it just a version he wrote himself? The latter makes sense for pretty much all algorithms in which you'd use often on a container, to the point you'd start wondering why the standard doesn't provide them built-in.

Re: Popular Myths about C++, Part 3

#70
post #64

Earlier quoted context omitted.

Does Torvalds write anything in C++? The kernel and git are in C

This is my point exactly. I'd consider both git and the Linux kernel tough projects, so the choice of the programming language is interesting. More so, given his stance on C++.

How come it's interesting ? Linus has always been a C -programmer, and kernel space is something where you want to keep things simple and access tha raw bits of things, so C is a natural choice.

And if you've been programming C for 20 years or more, writing git with C feels like a natural solution.

It's not always about writing stuff with the most high definition solution, it's more about expressing ideas with the tools you know also.

Post reply on HN