Why is C++ still a very popular language in quantitative finance?

  • I had to ask this question after reading the answers to What programming languages are most commonly used in quantitative finance? I understand that C++ programs can be optimized pretty well and are faster than anything else. But in this era, the performance of a program written in a language based on frameworks such as C# and Java can be pretty close to that of C++, while the maintenance cost of the program would be lower than the C++ one. But why is C++ still a very popular language in QF?

    IMO This question is inappropriate for quant.SE. You provide no evidence that it in fact *is* the most popular language. Please search the site for other programming questions to see how this has been discussed in the past.

    @Belmont I actually found one faq in this site that says C++ is the most popular one in QF. That led this question. But I don't mind moving this question to more appropriate site. Please do so if necessary.

    @Taesung: Could you give the reference? Or perhaps you should change the question from "still the most popular" to "still very popular".

    @vonjd Updated my answer with the reference.

    I am very tempted to close this question, and indeed there are a couple votes already to close it. **The question you cite already answers this question!** C++ is popular because (1) the boss pays people to use it, and (2) data vendors have, at least historically, only provided proprietary APIs in C++.

    I did read those parts of answers but I wanted to clarify or dig into more details. I think I got what I wanted so don't have any objection to close.

    @chrisaycock the previous question only answers this question tangentially. I agree it is borderline, but I believe this question can stand on its own, especially in light of the excellent answers given here which mention information not given in the previous question.

    @BlackJack where did you post the link to this question to get it so many views?

    I think he mainly posted the link here ( http://hackerne.ws/item?id=2934042) Interesting discussions there over this weekend. I guess chrisaycock was interested in this question after all.

    Java does its optimization while the code is running, meaning that you'll have a few moments of sub-par performance while the code is being analyzed. It that sense, C++'s execution speed is more "constant." C++ memory management is more convoluted, but you know where and when the overhead's coming. There is an optional garbage collector component in the new C++ standard; we'll see how much it's used.

    @Taesung Do you understand why we prohibit beginner questions? Because we don't want a couple dozen non-quants offering their totally speculative "answers". The SE is meant for people *in the industry*, not hacker wannabes who want to write about topics they clearly don't understand. Ever since your question hit HN, we've been slammed by rookies who want to debate C++ vs Java and who want to know how to break into the business. And I, the moderator, am receiving flags from anonymous newbies who don't like the way the C++ vs Java debate is going!

    @chrisaycock God forbid that somebody should flat THE MODERATOR!! ;-)

    Alright, this question has run its course. I find it embarrassing that our most viewed question on this site is just speculative and doesn't even represent what quants do. And on a related note, anyone who asks strangers on the Internet what language he should use is a terrible programmer.

  • The other posters have already noted that the prevalent use of C++ appears to be due to historical reasons and unwillingness to change. Those reasons aren't the ones that people should be applying. If you want real reasons to use C++, how about the following:

    • Powerful infrastructure. Take a look at Intel Parallel Studio for an example.
    • Performance compared to .Net or Java (see my course on HPC). When each array element access checks the bounds and throws exceptions, you know you're leaking CPU cycles there.
    • Parallelization. The C++ ecosystem has vastly superior paralellization in both 'blind' mode (OpenMP vs TPL's Parallel) and explicit mode (Intel TBB vs TPL)
    • Lots of SDKs, most notably CUDA, base their development on C/C++.
    • Possibility of invoking low-level CPU instructions (e.g., work with SSE intrinsics).

    On the other hand, C++ is

    • Extremely noisy. What with all the headers, include directives, friend class declarations, and myriads of other redundant things.
    • Has hard-to-use libraries (STL, Boost) with very cryptic, global-level mechanisms. Think bind_2nd :)
    • Editor support is vastly inferior compared to IDEA/ReSharper. Navigation, refactoring, analysis - all are weaker or non-existent. This is going to be improved in the near future for both VS and standalone editing.
    • Compiler errors are beyond cryptic. Clang attempts to fix it to some extent, but things are still cryptic, just not as abysmally bad as they were previously.

    And by the way, for the typical user, the performance difference between C++ and, say, C# won't be as pronounced.

    Thanks for your answer. Indeed, I didn't know there is still that much difference in performance. Then, I guess it's obvious to use C++ for especially QF systems.

    "When each array element access checks the bounds and throws exceptions, you know you're leaking CPU cycles there." This is not true. JIT will remove plenty of array bound checks. Pure computational performance (ignoring memory allocation/deallocation) under .NET runtime (ignoring vectorization) is pretty close to the performance of raw C++.

    I concur with Mehrdad wrt Java. I was easily able to engineer Java-based quantitative trading models that were just as fast as C++. I worked in both languages at different firms and can tell you that well-written Java can be as fast as C++.

    @Dave I suspect you've been comparing near-identical code, which doesn't reflect the fact that in C++ you can explicitly use SSE. I also suspect (haven't tested) that OpenMP beats Java parallelization (unless you handcraft it) just the same as it beats .Net's TPL.

    What do 'editor support' and 'compiler errors' have to do with the language? These are editor and compiler issues, respectively.

    @James let me generalize then: *all* editors are pretty bad and *all* compilers give unreadable error messages.

    @Dmitri Nesteruk 'in C++ you can explicitly use SSE' yes but you wouldn't really often, right? You would rely on the compiler to do so for you. And this is, what the Java user does.

    @user492238 I don't know about Java, but AFAIK .NET does not vectorize its operations to take advantage of SSE. No idea of Java does this or not. And the point is not just to use SSE yourself, but to take advantage of libraries written by people who *did*.

    @Dmitri Nesteruk Right. I was just wondering, if this argument counts pro or against C++? Since to interface a library API is easy and recommended - from within almost every language?

    @user492238 This is a Pro-C++ argument because the C++ compiler is supposed to vectorize more. I use a vendor-specific compiler (Intel), so I trust it to generate instructions taking advantage of my specific processor. Yes, performance will suck on AMD, but then I'm not sharing code :)

    @user492238 almost all C++ code I've seen that used SSE (graphics and physics engines) use compiler intrinsics. When I played around with SSE, I also used intrinsics.

    @Dmitri Nesteruk "Compiler errors are beyond cryptic", clang largely fixes this.

    @Dan Does the Quant community use Clang, though? I had the impression that most stick to Microsoft C++. I'm an Intel C++ user myself, but am very tempted by C++ AMP.

    "Has hard-to-use libraries (STL, Boost)" what? STL is not hard-to-use

License under CC-BY-SA with attribution

Content dated before 7/24/2021 11:53 AM