Rust vs C++: Which is better at speeding up Python?
As anyone who knows anything about programming languages will know, Python is far from the fastest option available. However, it makes up for its lack of comparative speed with its ease of access and ability to work in tandem with a variety of languages. The question then is, which is the best language to partner with Python for ultrafast code?
How about Rust? A software engineer for AI infrastructure fintech Trigo took to Github recently to describe his process of 'Making Python 100x faster with less than 100 lines of Rust'. While that certainly sounds very impressive at face value, C++ can make Python code even faster.
Using Rust to speed up Python
Using the crate Pyo3, it's possible to write Python code in a Rust module. With this method, the Trigo engineer wrote just six lines of Python code calling to 54 lines of Rust code. They previously implemented Rust in a few alternate ways as well that still resulted in increased speed, but the final method was over twice as fast as the second-fastest Rust implementation.
Given that Rust dominates the code, why not just write it all in Rust? The engineer notes that much of the calculation is done using complex Numpy libraries. A full scale migration to a new language can also be quite a time-consuming and intensive process, especially if it is ultimately unnecessary.
The engineer says that Rust "unlocks true native performance for everyday Python code, with minimal compromises." Rust acts as "building blocks;" Python keeps it all together.
However, Richard Hickling, a former Barclays quant and CEO of crypto trading and analytics firm ProfitView says that "you get the feeling he's really into Rust - as many are - and wanted that to be the solution." He concedes that this is not necessarily a bad thing as "many software engineering decisions are made that way: justifications for what you wanted to do in the first place."
Using C++ to speed up Python
A more established means of speeding up Python comes from utilizing libraries compiled with any one of the C languages. Indeed, the Numpy library used by the fintech engineer is built upon C.
In a webinar from ProfitView on combining C++ and Python for algorithmic trading, Hickling says that combining the two can make up for the compromises made when deciding on one over the other. One such method of doing so is by using Cython, a superset language of Python and C.
"In C++ you accept compromises like the need for row sets in your data," says Hickling. "You accept that because you want very high performance." Contrastingly, Python allows you to "see your data clearly" but "requires flexibility at the cost of performance."
Hickling says combining the two allows you to "have your cake and eat it too." In addition, the two languages are far more prominent in the finance space than Rust and prioritizing C++ can make you more employable in the financial services industry.
Which language would you prefer to use to write ultrafast code? Would you combine one of these with Python or code using Rust of C++ by themselves? Let us know in the comments.
Have a confidential story, tip, or comment you’d like to share? Contact: firstname.lastname@example.org in the first instance.
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.)