Take the 2-minute tour ×

"Programming in C++ is premature optimization"

Why does everyone do some people say this?

share|improve this question

closed as not a real question by Yannis Rizos Aug 24 '12 at 19:44


Not everyone says that, I don't. –  Mahmoud Hossam Dec 21 '11 at 22:37
People are lazy or can't learn the language to use it effectively. –  Coder Dec 21 '11 at 22:45
[citation needed] –  Nicol Bolas Dec 21 '11 at 23:55
First time I heard that –  Loki Astari Dec 21 '11 at 23:59
This is like taking two cargo-cult arguments and blending them together. It is more stupid than the sum of the stupidness of the two. –  Tamás Szelei Dec 22 '11 at 12:40
show 9 more comments

4 Answers

People often say this (somewhat tongue-in-cheek, in my experience) as a dig against C++ as a choice of language. People typically choose C++ because it is, in most cases, faster than an alternative like Python or Java. However, most of the time, the additional speed gained is not really needed. When a person says "Programming in C++ is premature optimization" they are saying that you could have programmed your application in some other language, been done much faster, then if you needed to make some part of your code really fast, you could simply code that part in C++ and call that routine from the other language.

share|improve this answer
Agreed with your interpretation, although since it is entirely possible in some cases to use a language that simply does not perform well enough to suit the task, I would say that it's not always premature optimization, but in fact a necessary optimization. –  Robert Harvey Dec 21 '11 at 22:56
I'd also argue that you have to consider the costs of using C++, which in my experience are not dramatically reduced from Java or C# or Python at all. –  DeadMG Dec 21 '11 at 22:58
Actually, you don't even need to mention the fact that if some part of the code needed to be made fast, then that and only that part could have been coded in C++. It is sufficient to just say that at the time of choosing which language to code in, you obviously did not know whether any part of the program would turn out to be performing below spec, so the choice of a quote - fast - unquote language at the expense of a more productive language was entirely unjustified to begin with. –  Mike Nakis Dec 21 '11 at 23:12
There are other reasons to program in C++, as opposed to, say, Java. In some ways, it's a more expressive (as well as clunkier) language. RAII is a very nice way to handle resources in general, and the template system is very powerful, to name two examples. –  David Thornley Dec 22 '11 at 18:34
add comment

Because they don't know how to program in C++.

Generally speaking, such people simply don't understand how to program safely (and quickly) in C++ and suggest that doing so takes up an inordinate amount of time or somesuch rubbish. If it really was significantly slower, then people would only do it to make their programs run faster, as C++ is easily the fastest mainstream language. That would make it an optimization- "I'd rather program in C#, but I have to use C++ because it's faster.", and usually premature because most optimizations are.

However, this is usually ignorant. C++ hasn't been a hassle to program in since shared_ptr and std::vector were invented.

Edit: Sorry, I wasn't sufficiently clear. I forget when speaking to non-C++'ers how little they generally know about the language, as I usually just stick in the C++ channel on SO. First, shared_ptr is hardly the only smart pointer, I use unique_ptr a lot more often, and Boost has more. Second, garbage collection is nothing like shared_ptr. Try to garbage collect GPU memory, or a file handle, or a database connection, or an HWND. Good luck. Third, the customizability of shared_ptr is vastly above what you can get from a garbage collected system. Recently, I replaced my memory allocations with a memory arena allocator that I wrote myself. It was like, a hundred lines of code and an hour to test. But I didn't have to re-write a single line of my shared_ptr code, because I just dropped in a custom deleter and it just worked. Try replacing your allocator or inventing a new smart pointer in C# or Java and tell me how long that takes. Oh wait, if you look at using, then even special language power can't make another kind of resource management work in C#. If garbage collection doesn't suit your needs, then you're screwed. That's not my definition of a higher-level language.

Now, I'll be the first to admit that I'm not on the cutting edge of languages like Python, Java, or C#. But as far as I know, Java and C# have nothing as strong as templates. Java's generics are hardly good for anything, and C#'s aren't that much better. For example, I use an expression template to automatically generate a finite state machine. I've never seen anything like that. If I was coding in Java, I'd have to code it manually.

share|improve this answer
@ChuckTesta: C isn't mainstream. Even if it was, it's horrendous unsafety would easily disqualify it from useful comparison. And also, C++'s templates and inline functions can be faster than C. And whoever you heard that from was a moron, or uninformed, or potentially both. –  DeadMG Dec 21 '11 at 22:55
@ChuckTesta: Yes, C++ is pretty much a superset of C, which allows for some handy shortcuts, although backfires with a requirement of greater knowledge. You can write C with templates and that alone will save you weeks of coding. –  Coder Dec 21 '11 at 23:08
shared_ptr is really nice compared to raw pointers, but it is still a hassle compared to what languages like Java and Python use. My feeling on C++ is that you spend a lot of effort trying to make it into a nicer language. Eventually, you realize you should just be using a nicer language. –  Winston Ewert Dec 22 '11 at 1:33
@Winston Ewert: I don't find shared_ptr any harder to use than references in Java. What makes you think they're more of a hassle? I actually find C++ semantics make a lot of sense. Its syntax could be better, but it can't because it has to be somewhat compatible with C. –  In silico Dec 22 '11 at 2:50
@Insilico, for one, its more of a hassle because you have to explicitly use it. In other languages you get everything shared_ptr offers on every pointer automatically without effort. There are a few cases such as reference cycles or getting a pointer to this where other languages make the task easier. So the shared_ptr stuff is just easier and tends to work a little bit better in other languages. –  Winston Ewert Dec 22 '11 at 4:48
show 16 more comments

Well, that's because programming in C++ does not magically give you any performance boost over, for example, Java. You need to do it very well, and doing that in C++ is definitely not an easy task. Also time consuming and programmer's time is not cheap...

share|improve this answer
It does, the start-up time is 5x faster, you don't need a stupid JRE and C++ apps are faster even for dumb implementations. And if you optimize the app, you can get a huge performance quite easily, 7x+. And it's not hard to optimize C++ apps. Choose the correct type of container, call vector::reserve, and you're set. –  Coder Dec 21 '11 at 23:00
Then show me 5x or 7x performance improvement with C++ over Java on shootout.alioth.debian.org without a lot of totally unreadable, carefully crafted code, as you said it could be gained quite easily. You can even contribute such program to beat "stupid JRE". –  Argbart Dec 21 '11 at 23:02
Google helps there: days2011.scala-lang.org/sites/days2011/files/ws3-1-Hundt.pdf There is even more than 7x fold increase in performance, and optimizations performed seem rather ordinary to me, as a C/C++ developer. But Java/.NET developers often complain because in C++ you have to think a little, not just write. Algorithm.Factory.Do.CoolSort –  Coder Dec 21 '11 at 23:15
I agree. I did not say, that Java is good for everything and C++ isn't good at all. If largest expense is applications running time, then definitely go for C++ ;) But the datacenters is not the whole world. –  Argbart Dec 21 '11 at 23:40
Sorry. I don't find writing C++ any harder than writing perl or python. It may take a lot longer to learn not only the language but where to find the appropriate libraries but once you have passed the curve it is no more difficult to use. –  Loki Astari Dec 22 '11 at 6:18
show 7 more comments

I haven't used C++ in a long time (and never templates), so this is from memory based on other questions on Programmers.SE and on StackOverflow:

Templates are handled at compile time, and C++ allows you do do some crazy tricks with complicated ones.

Because they're handled at compile time instead of runtime, all that execution is "optimized" away, leading to compiled code that could in some cases be faster than C, because the optimizations done are complicated and hard to to by hand.

And it's "premature" optimization because this is done before any testing for speed ;)

If anyone with experience wants to add/fix what I remember, please do. Don't just downvote.

share|improve this answer
Very true, +1. I agree. –  Coder Dec 22 '11 at 22:05
add comment
