The Shape of Code » A comparison of C++ and Rust compiler performance (2024)

Large code bases take a long time to compile and build. How much impact does the choice of programming language have on compiler/build times?

The small number of industrial strength compilers available for the widely used languages makes the discussion as much about distinct implementations as distinct languages. And then there is the issue of different versions of the same compiler having different performance characteristics; for instance, the performance of Microsoft’s C++ Visual Studio compiler depends on the release of the compiler and the version of the standard specified.

Implementation details can have a significant impact on compile time. Compile time may be dominated by lexical analysis, while support for lots of fancy optimization shifts the time costs to code generation, and poorly chosen algorithms can result in symbol table lookup being a time sink (especially for large programs). For short programs, compile time may be dominated by start-up costs. These days, developers rarely have to worry about small memory size causing occurring when compiling a large source file.

A recent blog post compared the compile/build time performance of Rust and C++ on Linux and Apple’s OS X, and strager kindly made the data available.

The most obvious problem with attempting to compare the performance of compilers for different languages is that the amount of code contained in programs implementing the same functionality will differ (this is also true when the programs are written in the same language).

strager’s solution was to take a C++ program containing 9.3k LOC, plus 7.3K LOC of tests, and convert everything to Rust (9.5K LOC, plus 7.6K LOC of tests). In total, the benchmark consisted of compiling/building three C++ source files and three equivalent Rust source files.

The performance impact of number of lines of source code was measured by taking the largest file and copy-pasting it 8, 16, and 32 times, to create three every larger versions.

The OS X benchmarks did not include multiple file sizes, and are not included in the following analysis.

A variety of toolchain options were included in different runs, and for Rust various command line options; most distinct benchmarks were run 10 times each. On Linux, there were a total of 360 C++ runs, and for Rust 1,066 runs (code+data).

The model The Shape of Code » A comparison of C++ and Rust compiler performance (1), where The Shape of Code » A comparison of C++ and Rust compiler performance (2) is a fitted regression constant, and The Shape of Code » A comparison of C++ and Rust compiler performance (3) is the fitted regression coefficient for the The Shape of Code » A comparison of C++ and Rust compiler performance (4)‘th benchmark, explains just over 50% of the variance. The language is implicit in the benchmark information.

The model The Shape of Code » A comparison of C++ and Rust compiler performance (5), where The Shape of Code » A comparison of C++ and Rust compiler performance (6) is the number of copies, The Shape of Code » A comparison of C++ and Rust compiler performance (7) is 0 for C++ and 1 for Rust, explains 92% of the variance, i.e., it is a very good fit.

The expression The Shape of Code » A comparison of C++ and Rust compiler performance (8) is a language and source code size multiplication factor. The numeric values are:

 1 8 16 32 C++ 1.03 1.25 1.57 2.45 Rust 0.98 1.52 2.52 6.90

showing that while Rust compilation is faster than C++, for ‘shorter’ files, it becomes much relatively slower as the quantity of source increases.

The size factor for Rust is growing quadratically, while it is much closer to linear for C++.

What are the threats to the validity of this benchmark comparison?

The most obvious is the small number of files benchmarked. I don’t have any ideas for creating a larger sample.

How representative is the benchmark of typical projects? An explicit goal in benchmark selection was to minimise external dependencies (to reduce the effort likely to be needed to translate to Rust). Typically, projects contain many dependencies, with compilers sometimes spending more time processing dependency information than compiling the actual top-level source, e.g., header files in C++.

A less obvious threat is the fact that the benchmarks for each language were run in contiguous time intervals, i.e., all Rust, then all C++, then all Rust (see plot below; code+data):

The Shape of Code » A comparison of C++ and Rust compiler performance (9)

It is possible that one or more background processes were running while one set of language benchmarks was being run, which would skew the results. One solution is to intermix the runs for each language (switching off all background tasks is much harder).

I was surprised by how well the regression model fitted the data; the fit is rarely this good. Perhaps a larger set of benchmarks would increase the variance.

The Shape of Code » A comparison of C++ and Rust compiler performance (2024)

FAQs

The Shape of Code » A comparison of C++ and Rust compiler performance? ›

showing that while Rust compilation is faster than C++, for 'shorter' files, it becomes much relatively slower as the quantity of source increases. The size factor for Rust is growing quadratically, while it is much closer to linear for C++.

How does C++ and Rust compare in performance? ›

Comparing Rust vs C++ performance

Rust's ownership model and borrowing system enable memory and thread safety without requiring a garbage collector, resulting in quicker and more predictable performance. C++'s more extensive library support can sometimes produce more efficient code than Rust.

Does Rust compile slower than C++? ›

cpp file. This compilation is done in parallel, but parallelism is imperfect. For incremental builds, Rust will take longer to compile than C++ (i.e. C++ wins). This is because Rust compiles one crate at a time, rather than one file at a time like in C++, so Rust has to look at more code after each small change.

What is the difference between C C++ and Rust? ›

While C++ has superior libraries to Rust, Rust has greater support for frameworks. C++ is superior for game development and object-oriented programming. Each have a sizable platform and community. Rust and C++ transitions are made simple via the CXX library.

What advantages does C++ have over Rust? ›

C++ can produce fast applications while spending less time on code compilation and execution than Rust. C++'s performance levels can be attributed to its vast standard codebase and smaller assembly code.

Is Rust the same speed as C++? ›

It is difficult to compare the two languages in terms of speed and performance directly. Generally speaking, Rust and C++ are comparable in terms of overall speed and performance, but when we take into account unbiased benchmarking, there are many instances in which Rust will perform even better than its counterpart.

Can Rust fully replace C++? ›

jasmer on Jan 30, 2023 | parent | context | favorite | on: Swift Achieved Dynamic Linking Where Rust Couldn't... Rust will not replace C++. Everything is thrown out the window, including developer productivity in order to provide the 'zero overhead runtime guarantee' feature.

Can Rust do everything C++ can? ›

Strictly speaking all turning-complete languages are equivalent, but there are plenty of things that are easy in C++ but more-or-less impossible in Rust (the opposite is true, too, though Rust can do things that C++ couldn't).

Why is Rust so slow? ›

In the end there's no such thing as "same constraints". Every language has been written with different expectations for what the user will do, and will be optimized accordingly. So yes, Rust is slow if you use it in a way that it wasn't designed for.

Does Rust use less memory than C++? ›

Go, Rust and C++ has minimal usage with C++ CPU usage being the least. C# memory usage is certainly higher in this scenario, followed by Go. But C++ and Rust's memory usage hasn't changed.

Is Rust a C++ killer? ›

The C++ Killers (Not You, Rust) | Hacker News. [Languages like Rust] do help you to write more features with fewer bugs, but they are not of much help when you need to squeeze the very last flop from the hardware you rent. I do think that Rust helps you squeeze out that last 1% of performance over C++.

Is Rust better than C++ for new projects? ›

Although Rust just about edges it on the memory safety front, C++'s manual memory management makes it a more flexible option suitable for software developers who value more control. Like Rust, C++ achieves its performance levels through low-level control and zero-cost abstractions.

Should I switch from C++ to Rust? ›

C++ boasts impressive concurrency features that are arguably more powerful and versatile than Rust's, however, it is more prone to concurrency errors and can be difficult to debug. Ultimately, if you'd prefer to use a concurrency system that is straightforward and secure, Rust is a more favourable choice.

What are the disadvantages of Rust language? ›

Steep Learning Curve: Rust is not an easy language to learn. It has a unique syntax and concepts such as ownership and borrowing that can be challenging for developers who are used to other programming languages. This means that there is a steep learning curve for developers who are new to Rust.

Is Rust more concise than C++? ›

Although the syntax of Rust is similar to that of C++, it's easier to learn and read. The type system of Rust vs C++ is more expressive, and its syntax is designed to be more consistent and predictable than C++, thus simplifying the code and catching errors during compilation.

Is Rust good for performance? ›

First, it's really important to say that both Go and Rust are absolutely excellent programming languages. They're modern, powerful, widely-adopted, and offer excellent performance. Rust is a low-level statically-typed multi-paradigm programming language that's focused on safety and performance.

Can C++ do everything Rust can? ›

Strictly speaking all turning-complete languages are equivalent, but there are plenty of things that are easy in C++ but more-or-less impossible in Rust (the opposite is true, too, though Rust can do things that C++ couldn't).

Should I use Rust or C++ for new projects? ›

Compared to C++, Rust promises more security. But, just like with any language, it's still likely to write insecure code in Rust if the developer isn't experienced yet. In general, Rust is considered to be a safe programming language that is often used in operating systems, file systems, and web browsers.

Top Articles
Best 15 Walmart Jobs for 2022
Which Crypto Has The Lowest Transaction Fees?
Ffxiv Act Plugin
Knoxville Tennessee White Pages
Moon Stone Pokemon Heart Gold
Wizard Build Season 28
Readyset Ochsner.org
Apex Rank Leaderboard
Unraveling The Mystery: Does Breckie Hill Have A Boyfriend?
Elden Ring Dex/Int Build
Skip The Games Norfolk Virginia
My.doculivery.com/Crowncork
Elizabethtown Mesothelioma Legal Question
Missing 2023 Showtimes Near Landmark Cinemas Peoria
Gino Jennings Live Stream Today
Munich residents spend the most online for food
Tamilrockers Movies 2023 Download
Katherine Croan Ewald
Diamond Piers Menards
The Ultimate Style Guide To Casual Dress Code For Women
Site : Storagealamogordo.com Easy Call
Is Windbound Multiplayer
Filthy Rich Boys (Rich Boys Of Burberry Prep #1) - C.M. Stunich [PDF] | Online Book Share
Integer Division Matlab
Horn Rank
Mals Crazy Crab
Cognitive Science Cornell
Craigslist Fort Smith Ar Personals
Jazz Total Detox Reviews 2022
The Clapping Song Lyrics by Belle Stars
Poe T4 Aisling
R/Sandiego
Kempsville Recreation Center Pool Schedule
Pfcu Chestnut Street
Beaver Saddle Ark
Log in or sign up to view
A Man Called Otto Showtimes Near Amc Muncie 12
Finland’s Satanic Warmaster’s Werwolf Discusses His Projects
The Minneapolis Journal from Minneapolis, Minnesota
Saybyebugs At Walmart
Gvod 6014
2007 Jaguar XK Low Miles for sale - Palm Desert, CA - craigslist
Candise Yang Acupuncture
Tlc Africa Deaths 2021
Youravon Com Mi Cuenta
Nope 123Movies Full
Kushfly Promo Code
Diario Las Americas Rentas Hialeah
Kidcheck Login
Marion City Wide Garage Sale 2023
Latest Posts
Article information

Author: Greg Kuvalis

Last Updated:

Views: 5645

Rating: 4.4 / 5 (75 voted)

Reviews: 82% of readers found this page helpful

Author information

Name: Greg Kuvalis

Birthday: 1996-12-20

Address: 53157 Trantow Inlet, Townemouth, FL 92564-0267

Phone: +68218650356656

Job: IT Representative

Hobby: Knitting, Amateur radio, Skiing, Running, Mountain biking, Slacklining, Electronics

Introduction: My name is Greg Kuvalis, I am a witty, spotless, beautiful, charming, delightful, thankful, beautiful person who loves writing and wants to share my knowledge and understanding with you.