Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Agreed. You would have to have to invest a considerable amount of time to beat the STL objects in terms of efficiency. I cant even think of a situation when you really could. Why try to reinvent the wheel? Seems like time wasted if you ask me.


The arguments I hear most frequently are the same ones that Linus jumps on in this thread: the syntax is obtuse, the code isn't portable, and the language isn't truly high-level (i.e. it doesn't have built-in garbage collection).

The first argument is (sadly) true -- C++ is ugly and complicated. A lot of that comes from the need for backwards compatibility with C (which isn't going to win any language beauty pageants, either), but a lot more of it comes as a consequence of the syntactic flexibility of the language.

The second argument is mostly bunk. A decade ago, it was difficult to find a standards-compliant C++ compiler; today, it's fairly easy. Sure, there are dark corners of the language where certain compilers get funky, but that can be said of FORTRAN 77 compilers, too. In general, unless you're forced to work with a old compiler, or an old, unmaintained package, it isn't very difficult to write cross-platform C++ code.

The third argument is just a straw man. Anyone who has read Stroustrup's book on the design of C++ knows why garbage collection and other "high-level" features weren't incorporated into the standard -- they didn't want to force users to adopt particular programming models or designs. Stroustrup has insisted that C++ be a flexible tool for many different types of programs and programmers; that attitude leads, somewhat inevitably, to complexity. But it makes me sad that so many people are bashing on C++ for not being "more like" other languages, when its biggest strength is probably its ability to mimic many features of those same languages, while still allowing for great efficiency.


C is beautiful if you're standing on a motherboard staring up at it. If you think mostly in terms of how hardware works, C is a clean, powerful language.

"The language isn't truly high-level" isn't the whole argument; which would indeed be a straw man. That's the main part that's tied to the "most C++ programmers suck" argument, though. He thinks the "pseudo high-levelness" of C++ encourages sloppy programming.

Also there's another argument in there: Object-Orientation is overrated and often causes as many problems as it solves.


Most programmers suck in every language.

If Linus' experience with C developers is that they are better than C++ developers, it's probably because he is familiar with the qualities of good C developers.


Here's the response from the thread:

LT> "You can write bad code in any language. However, some languages, and especially some mental baggages that go with them are bad."


Your third argument is fallacious. C++ is gradually incorporating everything you're talking about into the standard. Stroustrop has openly stated that lack of standardized threads were a mistake: you can't get much more environment-specific, model-specific than that.


Stroustrup was referring to the standard library, and he wasn't advocating the inclusion of threads at all costs.

C++ is still guided by the principle that you don't have to pay for what you don't use. That was the crux of my argument, and it continues to be true.


You haven't refuted Linus. You've explained why the world works the way Linus says it does. But the impact is the same.


to beat the STL objects in terms of efficiency

string s = "string literal";

vector<int> v = {1, 2, 3}; // C++0x feature

Any language with built-in string/array support will beat STL in initializing things, to start with. STL does dynamic allocation in these examples, in case you didn't know.


Are you sure about that? I don't really know the internals, but I would assume the implementation looks something like: Let the compiler put the array (of characters or numbers) somewhere where I can get them, and I'll take that pointer and keep it in my stack-allocated STL data structure.

Any references that say you're right?


The best reference is the source code.

Let's look at the string. In C/C++ there is no reliable and portable way for your program to find out whether some (char*) points to a constant string literal or not. So the only oprion for a dynamic string implementation is to always copy any data the constructor receives to somewhere else, normally to the dynamic memory. As a result, any initialization involves dynamic allocation and copying of data in C++.

At the same time languages that have built-in support for dynamic structures avoid this by creating proper structures in the constant segment. So a statement like string s = "abc" would do nothing but assign a pointer.

This is a huge problem in C++ and even C++0x doesn't solve it unfortunately.



I'm not convinced it's solving the problem I mentioned, but I'll try to figure out myself, since this becomes off-topic already.


Looks exactly like what was missing. Sweet.


You are certainly right, but I was simply comparing creating custom data structures in C vs. STL in C++ since that is what Linus was comparing. You are arguing that other languages which have built in static string/array support will be faster than C/C++ and I dont argue with that.


I think even in C or with C-ish programming with C++ you can achieve better performance (in return for some verbosity, of course).


Justify that statement with evidence. Why would you assume hash_map would outperform a C hash table, or vector outperform a C VLA?


The times you are likely going to be able to beat STL is if you:

1) Have a highly specific algorithm where you know your memory characteristics very well, or

2) You are doing lots of memory allocation/deallocation. For example, something like vector<vector<myclass>> will be nasty in STL.

Outside of that though it will be difficult. STL was designed with speed as the foremost objective. It is simply very hard to beat in MOST tasks (but not ALL). For example, Vectors dont do any bounds checking. Most STL vectors will have performance on par with a simple dynamically allocated array. Iterators are also not validated before accessing a container.

So I wont say they cant be beat. They certainly can. But how much is your time worth? I guess it depends on your objectives and how critical every ounce of speed is. I understand there are probably situations with some where this type of thing is life or death.


I think you missed the word "evidence" in my request. I could have predicted that you believed STL was faster. But "it's faster because it's supposed to be faster" is a weak argument.

Again I see people in this thread falling into the trap of conflating code reuse, a good thing, with C++, a dubious thing. Code reuse makes things faster, of course; it allows effort to be specialized. But C++ doesn't enable code reuse; it merely encourages it.


Here are some benchmarks on sorting:

http://theory.stanford.edu/~amitp/rants/c++-vs-c/

Besides that, custom C vs. STL comparisons would need to be done case-by-case. Admittedly, an all-encompassing statement "STL is always faster than C" is unreasonable. It depends entirely on the situation and what you are trying to do. I'm simply saying that it is very difficult and probably more time consuming than it is worth (for MOST cases) to try to beat STL in C++. When I used to do lots of graphics programming I tried (mostly out of curiosity), but I always ended up just using STL.

But maybe your experiences are different? Do you write your own arrays, hash maps, and sorting algorithms each time you use them? There are times when code reuse is A Good Thing. Like when the difference is negligible and it enables you to dedicate time to more valuable things, like building product features that users want. When it comes down to it, everything is always a matter of time.

Please explain what you mean when you say "C++ doesnt enable code reuse, it merely encourages it". Just curious..


He's not using "inline" for the C version, so it counts spurious function call overhead. He's also not holding constant the storage classes between C and C++; his C++ version arbitrarily gets to use optimal storage. This should be intuitive: bare C++ isn't faster than hand-coded C, and beyond that all you can compare is the quality of different libraries.

When I say "merely encourages", I'm trying to articulate the fact that most large C projects are composed of modules that implement different ADTs. Just because C doesn't call them "classes" doesn't mean that most C code isn't reusable.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: