Google Analytics Snippet

Thursday, March 29, 2012

If your software matters, you will write it in C++


Several years ago, at the 2008 ICFP, I was droning on about how awesome the F# programming language was the the language's creator, Don Syme. While Don appreciated my zeal, he put my irrational exuberance in check with a very insightful comment to the effect of "if your software actually matters, you will write it in C++".

The point isn't to claim that C++ is the "one true language", but rather that any benefit a programming language has for the programmer doesn't matter. If writing code in language X makes you 100x more productive but produces slow code, then all your users will notice is that your programs are slow.

Of course, like many of the great debates in computer science, there are a lot of nuances here about the importance of development time, language familiarity, platform adoption, and so on. However, I have provided an absurd proof backing up Don's comments for your amusement.

Proof: "If your software matters, you will write it in C++"

Definition: "software which matters" 

"Software which matters" is defined as:
- Software which is used by hundreds of millions of people daily.
- Software which generates billions of dollars in income annually.

Lemma: "software which matters" must be maximally optimized

"Software which matters" must be maximally optimized for { speed, ease of use, feature set }. Suppose by way of contradiction that some "software which matters" was not maximally optimized. It would be possible then, for an objectively better piece of software to exist. In such a world, over time, the objectively better piece of software would become more pouplar original software would cease to matter due to losing customers or revenue. [  ]

Proof

Step 1: Consider all programming languages requiring a virtual machine and/or runtime environment. (Java, C#, Python, and so on.) In exchange for niceties such as { memory management, runtime type checking, etc. } a non-zero amount of work must be done. For example, code must be JIT-compiled or a memory manager must be initialized. Such a non-zero startup cost violates the maximally optimized lemma, and rules out all such programming languages.

If you can shave 10% off of the startup time or memory footprint of software the matters, you will.

Step 2: C and C++ are the fastest known programming languages*. (* ignoring Fortran.)

Step 3: The only remaining languages for which "software which matters" can be written are C and C++. However, all reasonable C programs can compile using a C++ compiler. ("Reasonable" is intentionally undefined, like most of the C-standard.)

QED

59 comments:

  1. So how can you explain YouTube being written in a million lines of python. I think there are actually very few projects which can benefit significantly from the native speed of C++, but some of these don't come near to your definition of 'software that matters' - I'm thinking of games here specifically.

    Article on youtube's scalability: http://highscalability.com/blog/2012/3/26/7-years-of-youtube-scalability-lessons-in-30-minutes.html

    ReplyDelete
    Replies
    1. Everything that matters on YouTube runs on C or C++. Python pretty much just glues it together.

      Delete
    2. YouTube is a case-in-point. YouTube is indescribably slow even if you're sitting on the Internet backbone and need seatbelts to surf the web otherwise.

      Delete
    3. Curious... YouTube either has everything important written in C or C++, or it is indescribably slow because it is written in Python.

      Delete
  2. Hmm... I think software that does need so long to be written to be outdated the second it hit the market does not matter....

    Of course you are right where speed is really of importance but even here the days of big "optimizations" are long gone .... I can remember a time where you'd say something like
    "if this speed is important optimize in assembler" was the way to go a s a C/C++ programmer ...

    ReplyDelete
    Replies
    1. OK. accept that quick delivery. What will happen when same software will come into market with better performance? obviously your quick delivered product will go into dustbin. you take the example of Orkut and Facebook.

      Delete
    2. Better performance, feature set, support, or just about any other metric do not guarantee market share.

      "Good enough is the worst enemy of the best."

      Delete
  3. Your argument makes perfect sense assuming we have an infinite amount of time to develop software. You miss out a few important factors in your optimization: {delivery time; correctness; robustness}. We tend to need to optimize for those factors (in addition to ease-of-use) ahead of speed and feature set.

    And once you do need to optimize for speed, is it more effective to do that by switching to a lower-level language that gives you 10-50% performance boost, or focussing on improving your program's algorithm, which might give a much bigger boost?

    If you ever get to that point, and there is still a business case to improve performance, then it makes absolute sense to switch to C/C++.

    ReplyDelete
    Replies
    1. OK. accept that quick delivery. What will happen when same software will come into market with better performance? obviously your quick delivered product will go into dustbin. you take the example of Orkut and Facebook.

      Delete
  4. The problem with modern software is rarely runtime speed (though it still is in some instances, such as state-of-the art games). The problems are development, debugging, and maintenance. As an old-school, zillions of lines of C++ code developer, I know I can deliver faster code in C++. The question is: can I deliver a more useful overall product in C++? Sometimes the answer is still yes, but increasingly, languages like F#, Scala, Groovy, and Python are proving more effective.

    ReplyDelete
  5. There's no real-life software that's 100% optimized. That's only possible in theory.
    For the record, if you want 100% optimization, you should manually write and test machine code for every different CPU your application is supposed to run on.
    Why isn't that happening to "big" software? Because it's not worth it.

    There's a finite amount of resources you can spend on developing a software. Even for programs that "matter the most".

    ReplyDelete
  6. And using the better algorithms can often matter more than using the most optimized machine code. If the source code is cleaner, programmers find the fitting algorithms for certain sections with bigger probability (again, it's practically impossible that they use the most fitting algorithm for every problem).
    Therefore not choosing a language that needs to be 99.9% backward compatible with code written 40 years ago, and makes it very expensive (often impossible) for the developer to write clean code, can actually result in worse performance in the end.

    ReplyDelete
  7. s/worse performance in the end/better performance in the end :)

    ReplyDelete
  8. yeah, you can write your product in C++, the problem isn't that. the real problem is, can you maintaint it?

    that's where languages with succint principles or other "noble" goals enter the game.

    ReplyDelete
  9. Python with PyPy compiler can almost reach the speed of C, sometimes even faster, e.g. strings processing. And writing code in Python is way less painful. That's just one example

    ReplyDelete
    Replies
    1. It is only true if PyPy generate a C code and your C code is equal to PyPy code generated. if PyPy generate a bin to run the same C algorithm, it means your C code is not good enough. How about the compiler, what compiler? what operational system? It can make the difference, all difference.

      Delete
  10. to sum all of the comments:
    1) the definition of "software which matters" should be extended to include the rule
    - Software that can evolve with the changing requirements
    that rule measures how quick you can add features to your product. without that, you will quickly have an obsolete product.

    2) the lemma should be amended to:
    Lemma: "software which matters" must be optimized to meet requirements.
    the assumption of the original Lemma that optimization of every millisecond is the most important requirement is false.

    ReplyDelete
  11. There is one, always forgotten, beautiful language far easier than C++ and equally fast, when no even faster: Object Pascal. Be it either FreePascal or Delphi, today, you can do exactly the same you can do with C++ without all the terrible complexity of this language and with seemengly the same speed.

    ReplyDelete
    Replies
    1. Agreed. Been using it since the 90's. Still very productive.

      Delete
    2. Excepting the fact that develpoing in any IDE that supports delphi or its ilk is an abysmal, unintuitive and painfully slow/unintegrated experience. There's a reason it never gets an honorable mention.

      Delete
    3. Kay, I don't see it that way at all. It must be a subjective thing.

      Delete
  12. "If writing code in language X makes you 100x more productive but produces slow code, then all your users will notice is that your programs are slow."

    However, they will notice it for a hundred times as many programs.

    ReplyDelete
  13. Just created 2 solutions in Visual Studio: one with C++ and one with C#, added one function to each. Built the code. Result(C++) Build Failed, Linker errors, Result (C#): Build Succeeded, ran the program, recieved the output.."press any key to continue...". Good luck building useful systems in C++ and trying to get it to market in time, by the time your code builds successfully, I will have marketed my product and have had fair number of users using it. C++ may be good for system level programming but certainly not a choice for large scale web systems (with a few exceptions like Facebook whose PHP code is converted to highly optimised C code).

    ReplyDelete
    Replies
    1. OK. accept that quick delivery. What will happen when same software will come into market with better performance? obviously your quick delivered product will go into dustbin. you take the example of Orkut and Facebook.

      Delete
    2. yeah mate, I suspect that says more about you than about C++ :P

      Delete
    3. Facebook is developed in PHP. The fact that parts of the PHP code is now compiled into native, has little to do with C++ development. Almost the only C++ development going on at Facebook is the PHP -> native compiler, about 1% of the overall source code.

      PHP is an INTERPRETED language, way slower than precompiled languages like Java or C#, can be 50+ times slower than C++ for certain tasks, yet Facebook became the biggest social networking site relying only on this language at the time. They only began adding the PHP -> native compiler way after they became the most successful.

      The example of Facebook only shows that software that

      - Software which is used by hundreds of millions of people daily.
      - Software which generates billions of dollars in income annually.

      Can, and more and more often is, written in languages not focusing on performance.

      Delete
    4. So you don't know how to setup your C++ programming environment? What does that have to do with anything (other than you failed to RTFM)?

      Delete
    5. "obviously your quick delivered product will go into dustbin"

      Really? Obviously? You mean a web application deployed over a cluster of servers will go into the dustbin because a competitors version can start up and initialize itself in 1 min 24 seconds instead of 2 min 28 seconds?

      Delete
  14. If your software matters, you will write it in C. If for no other reason, performance.

    http://www.cherrystonesoftware.com/doc/AlgorithmicPerformance.pdf

    ReplyDelete
  15. When you thought all the bullshit around C++ starts to fade, you get blogs like these.

    While PHP, Python and Friends are no match for C++, C++ has had (historically) some interesting competitors.

    Competitors that compile natively, the same kind of code C++ generates. Code that is almost identical to C++, code which occasionally is faster than C++, and sometimes slower. Code that, if tended to, can be as fast as code created by a C++ developer after careful thought and benchmarking.

    So who is winning? It seems obvious there is no win in this situation.

    While I myself dislike C++'s cryptic syntax (which only increases on each spec update), I dislike even more the insane, childish, and stupid religious zeal emanated by some certain C++ developers.

    In a sense, I hate C++ because I hate these developers. Meanwhile, in the front lines C++ is still competing....and no one is anywhere near to winning anything.

    ReplyDelete
    Replies
    1. Funny, how different perspectives can be: I hate Phyton (and friends) because of their cryptic syntax (i.e., information about data types is not available/useful at coding/compile time), and the endless runtime errors that follow. There is nothing better than the c++ compiler yelling at you about stuff that would cause a runtime error in other languages.

      Delete
    2. What is so cryptic about c++? Templates?

      Delete
  16. This post was written very close to April the 1st. Maybe few days too early?

    ReplyDelete
  17. "Ignoring FORTRAN"
    Therein lieth the fatal flaw in your argument.

    ReplyDelete
    Replies
    1. "Ignoring fortran because it has 0 support, no development presence outside academia and has inconsistent implementation on the hardware people actually use"

      But you couldn't be bothered to fill in obvious blanks, could you?

      Delete
  18. I've been unable to find any flaws in your argument.
    I applied it to this site.

    A: Software that matters is written in C++
    B: Blog delivered via HTML.
    C: Therefore, blog doesn't matter.

    Yep, your case still holds.

    ReplyDelete
    Replies
    1. Sure - but I'll bet the server sending the HTML was Apache - and thus written in C.

      Delete
    2. HTML is a programming language?

      Delete
  19. Software that matters.... used by hundreds of millions of people daily, ...generates billions of dollars in income annually.

    Hmm... let's see...
    - mission/safety critical software (an awful lot of Ada.. remember Ada?)
    - telephone switching software (Erlang maybe?)
    - financial transaction processing software (AQMP message switching software, again, an awful lot of Erlang)

    ReplyDelete
  20. Languages are tools. No one is asking you to choose one and forsake all others. It's not a marriage FFS. They are tools in your toolbox. The argument is essentially "hammers suck, you should only use a screwdriver". And that will work, until you need a hammer. I have been programming in C++ for 25 years, and I wouldn't even think of writing a GUI in C++. Your principal of optimality is a fallacy...there is such a thing as 'fast enough'.

    ReplyDelete
  21. C++ is one of my favorite languages and I must say this article is out right absurd. Let's consider why:

    1) By this proof machine language really is the only way to go. You can optimize it more...
    2) Like others have said time to market is huge. And I am sorry your example of "what about when a more optimized product comes to market" is bogus. First, once you have customers there is a certain amount of vendor lockin. This is even more true when you have a hardware component. Are there cases of a better performing product taking over a market place? Sure. But if you are to market first and performance is not seen as a problem then when your competitor finally gets to market they will be dead in the water. Ever wonder why Ubuntu isn't over taking windows?
    3) You talk like optimizing products is the easiest thing in the world. In my day job I work in HIGHLY complex products and I'm sorry anyone who thinks they are even close to optimized is fooling themselves. I do embedded programming but still we often make choices of maintainability over performance because maintainability matters a TON.
    4) In certain cases (often tight loops) java has been shown to be faster than c because of some run time optimization it can make on how it runs the code. Is it perhaps possible to optimize your code so much to at least match this performance? Probably, but that's going to take you a LOT longer...

    If you need more performance then sure C/C++ probably is the language for you. But maintainability of code is EXTREMELY important. A major memory leak in your code is going to make your program much slower than Java ever will. Java is slower than C++, just as c++ is slower than C, and c is slower than machine lang., but the reason is more of the designs these languages promote than anything else. Higher level languages equal easier to maintain. Its a design choice just like everything else. I love c++ but in some cases it's the WRONG choice... Saying maintainability doesn't matter just shows a lack of real world commercial experience..

    ReplyDelete
    Replies
    1. That C++ is inherently slower then C is a canard. C++ is the same as C, except when you are doing something that C cannot do. In those cases, it is faster than a C implementation that could emulate the same feature.

      You touch on the real performance issue - but do not illuminate it. A truly skilled coder can produce a complex program in C++ that will run as fast or faster than an equivalent C program, and be more maintainable. The real problem is that the skill required is perhaps double or more to produce truly good C++.

      Delete
  22. *yawn*

    Erlang ;)

    ReplyDelete
  23. Sorry, but I also accept this article only as a joke on 1st of April...

    ReplyDelete
  24. Wow... many fallacies in this "proof."

    First of all, in your piece and your follow-up, you appear to have an implicit assumption that all "software that matters" is THE product or THE major part of a product, the cost of which is the major defining factor for a purchase decision. There's lots of "software that matters" that plays an adjunct, facilititative, role in a product. Consider a service accompanying a piece of hardware. Or secondary functions or services provided for a suite a programs.

    Second, you lump the cost of JITing or initializing a MM - a one-time, initialization activity -- into the overall performance of a piece of software. This is just terrible engineering. One does not optimize one-time activities, such as initialization, unless they cause problems to the user.

    Third, you argue that C and C++ are the "fastest" languages, citing experiments with GCC and Mono. All the measurements indicate is that the GCC compiler is more efficient than the Mono runtime environment. Remember "YMMV"?

    Finally, using those speed statistics you cite, you dismiss the significantly BETTER results for Fortran. C'mon... if you're going to make an argument, MAKE it: We should write all our Software That Matters in FORTRAN! What? You say FORTRAN is unsuited to many tasks, difficult to use, and outdated? Yeah... that's what I would say about C and C++.

    QED.

    ReplyDelete
  25. Just more C evangelizing.
    Any compiled language will perform just as well.
    Including VB/VB.Net.
    As long as the compiler isn't a piece of junk, the resulting machine code is just as efficient. Often indistinguishable.

    C was created by lazy assembler coders who didn't want to bother learning new mnemonics every time a new processor came along, and back then it was pretty frequently.

    Unless your code is Very well documented, rare unless your employer demands it, even the original programmer can have trouble figuring out what the heck some routine is doing just a few weeks later.

    C languages might be OK if you're developing operating systems or some major retail products if that makes you feel more "special". But for general business applications, even VB is a better option for long-term maintenance of the code.

    And for the record, Yes, I DO know C/C++/C#/Javascript/ASP, just don't make me work on some other idiot's code.

    ReplyDelete
  26. Good software is written by good programmers - and there are bad C/C++ programmers who could write a program that is slower than any interpreted language out there.

    ReplyDelete
    Replies
    1. > Good software is written by good programmers...

      Bingo. Notions of "productive" in discussions like this are too often clouded by multiple fallacies.

      Software That Matters must be written to meet or exceed operational, performance, scalability, and security requirements

      Software that matters needs to be written in a manner that passes code review, and meets the org's standards for static code analysis and such.

      So in almost every case, Software That Matters must be written by highly experienced, self-motivated, well-rounded coders who can and do write well-designed, high-performance, maintainable code as a matter of professional pride, regardless of business objectives.

      I work in a poly-lingual shop with a group of nearing-or-have-alerady-reached-middle-age (read "highly-experienced") developers. Most of our work is in C++, Delphi, Java, and C#, with a number of others employed when it makes sense.

      Some of these folks are scary fast with C++, but a lot of that speed comes from years of developing and refining truly useful, highly granular libraries of code that come not from a vendor, but from individual and organizational maturity. And also from knowing the tools extremely well. Contrary to at least one comment above, the Delphi (and C++ Builder) IDE is a marvelously productive environment to work in. So are many others -- especially when you take the time to master them.

      Sometimes language choice really matters, but this is the exception, not the rule. For example, our systems somtimes include Windows services. We typically need those to be small, fast, and architecturally simple - a single .EXE with no runtime dependencies is usually desired if not required. So for that kind of work, C/C++ and Delphi are great choices, and we'll not even consider C#, Java, VB, or any of the scripting languages.

      But for all that, the most important part of a software development project is the software developers. Good ones will produce good work. Rather than focusing on language or technology, we serve ourselves well to focus instead of fostering a culture of excellence at our craft.

      Delete
  27. Keep in mind that Microsoft neglected C++ and the tools sets that it needs to be productive for a decade in favour of pushing the .NET mess.

    Also, applying sound OOP with C++, maintenance shouldn't be an issue.

    and we should be using 'D' language syntax, and c/c++ should only be an intermediatery language to feed to the compiler.

    ReplyDelete
  28. If your website matters, it will work in IE. :-P

    ReplyDelete
  29. > It would be possible then, for an objectively better piece of software to exist. In such a world, over time, the objectively better piece of software would become more pouplar original software would cease to matter due to losing customers or revenue.

    You have left out the assumption that the cost of switching is zero. It is quite far from 0 actually, in some cases so fundamentally much that bad products preside over good products significantly.

    ReplyDelete
  30. C/C++ is the language to develop operational systems, compilers, linkers, drivers, and every thing else. In the pass, write a bakery software was more productive in Java,Delphi, .Net, whatever. But today, we have Qt, Gtk, stl, boost, and a lot of libraries to give support.

    The maintenance, it depends on how good programmer you are, and how good is the analysis made over the software.

    Every thing is relative. But I thing I will never change C/C++ for any other kind of language.

    ReplyDelete
  31. If your project matters, you won't care about programming languages, use whatever works best for you. Your users certainly won't care either.

    ReplyDelete
  32. Most software that matters was written in COBOL, years ago, by people who never took any Computer Science classes.

    ReplyDelete
  33. So, how come your mantra isn't "Software that matters is written in assembler"? C++ is definitely a dog in comparison.

    ReplyDelete
  34. Great article! This is something I often ponder myself. Many times, I have given up on software, due to poor performance; substituting the tool for something that does the same thing, only written in a much "faster" language.

    The sad thing is, most of the arguments against this article is "But C++ is so much harder to debug and maintain!" Sorry guys, I hate to burst your bubble, but the ONLY thing that makes ANY code/software more difficult to debug/maintain is POOR PLANNING/POOR DESIGN. NOT the language it is written in.

    I dare you to try and prove me wrong otherwise.

    I wonder if programmers even use a pencil/paper anymore. Does anyone here write pseudo-code anymore? UML anyone? Too often, I see programmers struggling, trying to solve simple problems; only because they did not sit down and think it through first.

    The most important thing I was ever taught about programming is this:
    Before you write any (and every) line of code, you ask yourself one question-"Why?" If you can't answer that question, you'd better stop typing right there.

    The more thought one puts into the design/planning of an application, the less time they will spend both PROGRAMMING the application AND DEBUGGING the application.

    If you still don't believe me, here's some food for thought: Computers can be programmed to "write" code but cannot be programmed to "design" code. Take that as you will.

    ReplyDelete
  35. If your blog post matters, you will check it for grammar and spelling before publishing...

    ReplyDelete
  36. "When you thought all the bullshit around C++ starts to fade, you get blogs like these..."

    You are obviously suffering under the delusion that the software you write is the stuff that matters. It is quite obvious that in most of the cases expressed here it seems that the software involved that really really matters is PHP, Python, F#, etc. And I suppose these were written in Perl????

    ReplyDelete
  37. If your software matters you will write it in English (or your native), mathmatical equations, or models. You then implement it in a programming language (or not), which usually (with the exception of pure math) is sub optimal. For software products my view is Marketshare (for the producer) and Total Cost of Ownership (for the user) are the biggest things, as long as the product meets minimum requirements.

    ReplyDelete