Discover your dream Career
For Recruiters

COMMENT: C++ or Java? Which is best for trading systems?

You’ve done your data science work in Python. The numbers make sense; the results are nontrivial. Now you just need to put them to practice. You want to obtain the same outstanding results – only now on new data arriving in real time, twenty-four hours a day, seven days a week.

Should you just clean up your Python code from the Jupyter notebook (make it work, make it right, make it fast), package it as a collection of modules – as a library – and put it into production for real-time use by others?

Sometimes the answer is yes. More often than not, though it is 'no.' Python is not an ideal language for production use. It is slower than many other languages. There’s that global interpreter lock (GIL). Python’s duck- and dynamic typing are a double-edged sword: those annoying checks that you were happy to forego while prototyping could come back to bite you when a corner case surfaces in real-life use. And they always do surface.

So, you begin to ponder another question: C++ or Java?

C++: The hardest programming language of the lot

C++ is arguably the toughest programming language, fit only for the smartest people. The C++ assembly code is closer to the metal. C++ has been the favourite of hardcore developers (and quants) ever since Bjarne Stroustrup created it in 1985.

A C++ pointer is the address of a variable in memory. But with the great power of low-level memory access comes the great responsibility for managing the lifetime and ownership of objects. The side effect is this: you think you are debugging a Monte Carlo engine or PDE solver, whereas in actual fact you are spending most of your time debugging memory access. Especially if you are new to C++; and it takes time, potentially years, to get quick at this.

Smart pointers make the task of memory management somewhat easier but do not eliminate the problem completely.

Java: The dummies C++?

Java was created by James Gosling as a dumbed-down version of its ten-years-senior cousin for fancy television sets.

Java’s garbage collector does memory management for you. It works in two simple steps. During the mark step it identifies which pieces of memory are in use and which are not. During the sweep step it removes the objects identified during the mark step as garbage collectable.

In most non-real-time applications the garbage collector shouldn’t be a problem. However, if you are writing a high-frequency trading system, the garbage collector may cause intrusive latency spikes.

There are options to help mitigate this. Oracle’s native garbage collector is highly configurable. Azul Systems, Inc’s Zing is an alternative Java Development Kit (JDK) that eliminates GC and application timeout issues immediately without the need to keep tuning the Java Virtual Machine (JVM).

Nondeterministic latency is also a concern in Java when it comes to real-time systems. However, there have been advances. The high-performance inter-threading messaging library, LMAX Disruptor, has brought this latency down through mechanical sympathy for the hardware that it is running on and through being lock-free.

The fact that Java compiles to bytecode and runs on a virtual machine means that it is highly portable and can run on Windows, Linux, and MacOS with minimal tweaking. C++ source code is, in theory, portable, but compilers may behave differently on different systems, so a lot more tweaking is required.

Java’s rigid syntax makes it easier for programming environments, such as Eclipse and IntelliJ IDEA to support powerful refactoring. But this increase in productivity comes at a price. Java’s generics are more syntactic sugar than C++’s powerful templates.

Both languages have advanced but legacy systems are stuck in the past

Both C++ and Java have advanced in recent years. C++ went through several revisions, namely C++03, C++11, C++14, and C++17. New features are being considered for inclusion into the forthcoming C++20. Among the recent C++ features are the move semantics, unified initialisation, lambda functions, multithreading and the memory model, regular expressions, standardised smart pointers, hash tables, std::array, reader-writer locks, fold expressions, structured binding declarations, parallel algorithms, and many more features that C++ programmers will find very exciting.

Post C++11 is very much a different language, sometimes referred to as modern, as opposed to classical, C++.

Java also developed through versions 7, 8, 9, 10, 11, and 12. It now features lambda expressions, functional interfaces, default and static methods in interfaces, Java Stream API for bulk data operations on collections, Java time API, collection, concurrency, I/O API improvements, and more.

However, new language features may not be an option for you if you are faced with the all-too-common task of supporting legacy code on a legacy system. In this case, the real question is whether you should support the existing codebase or rewrite the product using modern versions of the languages and modern development tools? This is one for you to decide on a case by case basis. 

Paul Bilokon is a founder of The Thalesians. The Thalesians are an Artificial Intelligence (AI) company specialising in neocybernetics, digitaleconomy, quantitative finance, education, and consulting. The are experts in (and run courses in)  the application of Machine Learning (ML) techniques to time series data, particularly Big Data and high-frequency data. Our areas of expertise also include the mathematics of ML, Deep Learning (DL), Python, and kdb+/q. A former quant and algorithmic trader at Deutsche Bank, Citi and Nomura, Paul also lectures part time at Imperial College London.

Have a confidential story, tip, or comment you’d like to share? Contact: in the first instance. Whatsapp/Signal/Telegram also available.

Bear with us if you leave a comment at the bottom of this article: all our comments are moderated by human beings. Sometimes these humans might be asleep, or away from their desks, so it may take a while for your comment to appear. Eventually it will – unless it’s offensive or libelous (in which case it won’t.)

AUTHORPaul Bilokon Insider Comment
  • Ia
    Ian Joyner
    24 February 2022

    C++ and Java are not the only options.

  • Ia
    Ian Joyner
    24 February 2022

    "C++ is arguably the toughest programming language, fit only for the smartest people."

    That is the egotistical nonsense I addressed in this article: https://www.efinancialcaree...

    C++ appeals to those who think programming is machine oriented. Programming is rather problem oriented and should be abstracted from any issues of any particular machine. It is this poor mix, that makes C++ hard to learn. It is a mix of oil and water.

    I'll quote Alan Kay, yet again, since he sums it up very well.

“This is the most pernicious thing about C++ and Java in that they think they’re helping the programmer by looking as much like the old thing as possible but in fact they’re hurting the programmer terribly by making it difficult for the programmer to understand what is really powerful about this new metaphor.”

    "The C++ assembly code is closer to the metal. C++ has been the favourite of hardcore developers (and quants) ever since Bjarne Stroustrup created it in 1985.

    Stroustrup hardly created C++. It was mixing the oil of Simula (OO) with the water of C. He did this with #defines to create 'C with Classes'. I did the same in Burroughs ALGOL 'ALGOL OO style' using the original define mechanism in Burroughs ALGOL (C did not even invent defines, that came from Burroughs ALGOL from a suggestion of Donald Knuth).

    Even C itself was not a designed language but a compiler adaptation from BCPL, as an expedient to do Unix.

    C and C++ have set this profession back decades, both technically and in mentality.

  • Da
    16 July 2019

    Take a look at Peter Lawrey's Chronicle.

  • Ts
    Tsc Admin
    16 July 2019

    Java is better for low-performance systems, and C++ is better for high performance systems. Java encourages bloated and inefficient code, and its garbage collection causes long pauses that are unsuitable for low latency trading.

  • Bo
    11 July 2019

    I struggle to believe the author of this article has ever used any of those 3 languages “in anger”. Complete nonsense.

Sign up to Morning Coffee!

Coffee mug

The essential daily roundup of news and analysis read by everyone from senior bankers and traders to new recruits.

Boost your career

Find thousands of job opportunities by signing up to eFinancialCareers today.
Recommended Articles
Recommended Jobs

Sign up to Morning Coffee!

Coffee mug

The essential daily roundup of news and analysis read by everyone from senior bankers and traders to new recruits.