"Programming in C++ is premature optimization"

Why does everyone do some people say this?

  • 17
    Not everyone says that, I don't. Commented Dec 21, 2011 at 22:37
  • 6
    People are lazy or can't learn the language to use it effectively.
    – Coder
    Commented Dec 21, 2011 at 22:45
  • 20
    [citation needed] Commented Dec 21, 2011 at 23:55
  • 14
    First time I heard that Commented Dec 21, 2011 at 23:59
  • 8
    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. Commented Dec 22, 2011 at 12:40

4 Answers 4


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.

  • 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. Commented Dec 21, 2011 at 22:56
  • 3
    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
    Commented Dec 21, 2011 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
    Commented Dec 21, 2011 at 23:12
  • 2
    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. Commented Dec 22, 2011 at 18:34

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.

  • 8
    @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
    Commented Dec 21, 2011 at 22:55
  • 5
    @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
    Commented Dec 21, 2011 at 23:08
  • 7
    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. Commented Dec 22, 2011 at 1:33
  • 3
    @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. Commented Dec 22, 2011 at 4:48
  • 3
    -1 for the fact it calls people who find C++ less productive "ignorant" and says "they don't know how to program C++" and calls their opinion "rubbish". Commented Dec 22, 2011 at 4:57

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...

  • 5
    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
    Commented Dec 21, 2011 at 23:00
  • 1
    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
    Commented Dec 21, 2011 at 23:02
  • 2
    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
    Commented Dec 21, 2011 at 23:15
  • 1
    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
    Commented Dec 21, 2011 at 23:40
  • 1
    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. Commented Dec 22, 2011 at 6:18

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.

  • Very true, +1. I agree.
    – Coder
    Commented Dec 22, 2011 at 22:05

Not the answer you're looking for? Browse other questions tagged or ask your own question.