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

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: