0% found this document useful (0 votes)
55 views5 pages

Rust Vs C A Battle of Speed and Efficiency

The document compares the performance of the programming languages Rust and C++. Through experiments implementing sorting algorithms like counting sort and bubble sort, it analyzes variables like memory usage, compilation time, and execution time. The results show that C++ has faster compilation times and execution times compared to Rust, though the differences are small. Overall, Rust has advantages like improved memory safety but C++ performs better in situations requiring high performance.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
55 views5 pages

Rust Vs C A Battle of Speed and Efficiency

The document compares the performance of the programming languages Rust and C++. Through experiments implementing sorting algorithms like counting sort and bubble sort, it analyzes variables like memory usage, compilation time, and execution time. The results show that C++ has faster compilation times and execution times compared to Rust, though the differences are small. Overall, Rust has advantages like improved memory safety but C++ performs better in situations requiring high performance.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

ISSN: 2834-7706

Research Article Journal of Mathematical Techniques and Computational Mathematics

Rust vs C++, a Battle of Speed and Efficiency


Vincent Ng*
Computer Science St Joseph International School Kuala *Corresponding Author
Lumpur, 57000, Malaysia. Vincent Ng, Computer Science St Joseph International School Kuala Lumpur,
57000, Malaysia.

Submitted: 2023, May 18; Accepted: 2023, Jun 17; Published: 2023, June 21

Citation: Vincent, Ng. (2023). Rust vs C++, a Battle of Speed and Efficiency. J Math Techniques Comput Math, 2(6), 216-
220.

Abstract
This study compares the performance of two excellent options for system-level development, the programming
languages C++ and Rust. Through a series of tests and experiments using socket servers and various algorithms, this
experiment analyses the speed and efficiency of code written in each language by looking at variables like memory
management, and compilation times. The findings reveal that Rust has a number of advantages over C++, including
quicker compilation times, improved memory safety, and in many situations equivalent or better performance. C++
still performs exceptionally well in several fields, nevertheless, such as low-level hardware programming and backward
compatibility. Overall, the results indicate that Rust is a strong candidate for systems programming jobs, especially for
new projects or those requiring a high level of performance and security.

Keywords: Rust, C++, Algorithms, Benchmarking, Compiler, Memory Safety.

1. Introduction and implementation complexity (lines of code) will be used to


Rust and C++ differ from each other primarily in terms of assess which language is most appropriate. In this project, I’ll
efficiency and speed. Rust has established itself as a rival to C++ write an identical programme in both C++ and Rust as a case
over the past few years. These factors are particularly crucial study. I’ll quantify software complexity using LOC (lines of
for system-level programmes like socket servers, encryption code).
algorithms, and even hardware, where even the smallest
performance improvements can have a significant impact on 1.5 Outline
overall system performance. Understanding the performance In Chapter 2 ‘Method’ I detail the exact implementations of our
disparities between Rust and C++ and how these languages programs and how the results are produced. Chapter 3 ‘Results’
compare in terms of their capacity to generate quick and effective presents these results which are then discussed in Chapter 5 ‘The
code is therefore of great importance. Analysis’. Chapter 6 ‘The Conclusion’ presents my conclusion.

1.1 Purpose 2. Experiment


This project’s main purpose is to compare how well Rust 2.1 Method
performs in direct comparison to its peers such as C++ by To compare the two languages, testing sorting algorithms like
developing similar applications and pushing Rust and C++ to Counting Sort and Bubble Sort against large inputs will be one of
their limits with sorting algorithms. our methods to measure software complexity and performance.
To avoid advantages/disadvantages given by certain external
1.2 Problem Statement libraries in each respective language, all code will be written
The aim of this piece of research is to answer the question: Is using their default built-in libraries.
Rust or C++ better for performance-based applications?
2.2 Measurements
1.3 Context In this experiment, I will be using the time unix command built-
My machine is a 2020 Macbook Air with the M1 Chip (8GB in Linux with some flags to get the compilation time, as for CPU/
ram), I will be using Rust 1.66.0 and C++ 17 to do the tests. The memory consumption measurements I’ll be using Gtime on Mac
M1 chip has 8 cores which are sufficient for experimenting. (GNU-time). As for LOC, I’ll be just simply counting the lines
of code used to create the program (not including blank lines and
1.4 Project Scope comments). The exact command used can be seen in Figure 1.
Performance measurements (execution time, compilation time)
J Math Techniques Comput Math, 2023 Volume 2 | Issue 6 | 216
To ensure maximum performance and disadvantages of using accurate representation the same programs will be run multiple
system libraries for benchmarking, I decided not to include times and an average will be taken to compare. To summarise,
benchmarking the program in the code itself and instead use measurements will be separated into two categories, Category
an external program to benchmark the program as I believe it 1, Software Complexity (LOC), and Category 2, Software
is more accurate and fair that way. Other than that, to get an Performance (Compilation Time, Execution Time).

Figure 1: Command Used for Measurements


2.3 Code Used output array. A separate output array is created to store the sorted
2.3.1 Counting Sort elements, the input array is iterated over and over resulting in a
I will first explain Counting Sort before showing the code. sorted result array, refer to the pseudocode to see how it should
Counting Sort has a time-space complexity of O(n+k). Counting look in code. For my Rust code for this implementation refer to
Sort works by counting unique elements in an array; the Figure 2. As for my C++ implementation look at Figure 3. The
algorithm makes sure that each element is in a specified range, it way I came out with these pieces of code is I would write my own
then creates a count array to store the number of occurrences of version of the sorting algorithm and consider other developers’
each element in the input array. The count array is then modified implementation of a similar algorithm and make changes to my
such that each element at index I stores the sum of the previous code to fully optimise the program.
elements, giving the starting index for each element in the sorted

Figure 2: Pseudocode for Counting Sort

Figure 3: Code for Rust Implementation of Counting Sort

J Math Techniques Comput Math, 2023 Volume 2 | Issue 6 | 217


Figure 4: Code for C++ Implementation of Counting Sort

2.4 Bubble Sort same checks until in the end there is a sorted list left. Refer to
Like previously I will explain the Bubble Sort algorithm before the pseudocode below to get a better view of how it should look
I show the code; Bubble sort works via comparing adjacent in code. The code for Rust and the C++ implementation can be
elements in an array and swapping them if they are not in the seen below in the pseudocode
correct order. The algorithm iterates over every index with the

Figure 5: Pseudocode for Bubble Sort

J Math Techniques Comput Math, 2023 Volume 2 | Issue 6 | 218


2.5 Results/Discussion outperform Rust even when the C++ code is not as optimised
Below are the results of the experiment, it is evident that C++ as the Rust code. This can be regarded as the fact that Rust
is slightly more efficient than Rust in terms of Compilation implements a lot of memory safety features such as bound
Performance and Execution Performance. Though the difference checking, ownership, mutability etc. These functions can lead
is not as much, it can make a massive difference in performance to slightly slower execution times compared to C++ with less
for programs with larger scales like a search engine or a machine memory safety, compilation times are also affected by the same
learning algorithm. In every single algorithm, C++ managed to factors.
Languages Memory Used Memory Used Compilation Time Execution Time LOC
(kb) (kb) (seconds) (seconds) (Lines of Code)
(Compilation) (Execution)
Rust 109392 1621 0.82s 0.011s 26
C++ 77301 1504 0.14s 0.005s 49
Table 1: Results for Counting Sort Algorithm

As seen in terms of performance for the Counting Sort algorithm numbers. Below is the table for the Bubble Sort algorithm. Once
C++ is outperforming Rust by a little bit, this difference expands again C++ is outperforming Rust in terms of performance but in
more though with bigger volumes of inputs example 1,000,000 this case, the difference is actually

pretty small which shocked me as I expected more difference has so many built-in functions in the standard library that just
in terms of execution time, but turns out they’re pretty close. In makes writing code for these types of programs easier.
terms of code complexity, Rust wins against C++ because Rust

Languages Memory Used Memory Used Compilation Time Execution Time LOC
(kb) (kb) (seconds) (seconds) (Lines of Code)
(Compilation) (Execution)
Rust 112384 1584 0.655 0.003 27
C++ 66576 1424 0.499 0.002 38

J Math Techniques Comput Math, 2023 Volume 2 | Issue 6 | 219


To justify why Rust always uses significantly more memory threading is difficult in Rust because of ownership limitations
when compiling, it’s because when Rust compiles to an and mutability. As there are fewer memory safety mechanisms in
executable it optimises the program for runtime execution which C++, it enables greater flexibility and gives programmers more
is why it uses more system resources compared to C++ whose control over memory management and optimisation. In low-
compiler doesn’t optimise its executable as much as Rust. Other level applications, where programmers must optimise code for
than that, as mentioned before Rust implements many memory certain hardware, this flexibility might be helpful.
safety functions which is one of the main factors into why it
takes slightly longer to compile and execute. Rust’s "cargo" tool, which comes pre-packaged with building,
testing, and benchmarking, makes development simpler and is
3. Summary another benefit for high-level complicated systems [2]. The greater
To answer the question of which language is better for selection of libraries, tools, and frameworks offered by C++,
performance-based applications, both Rust and C++ are fantastic however, makes it more appropriate for particular applications.
options for performance-based applications, but each language In the end, the demands of the project and development team
has advantages and disadvantages. I believe that C++ is better will determine whether Rust or C++ should be used. Developers
suited for low-level performance-based applications like firmware ought to take into account elements like performance, safety,
for washing machines, whereas Rust is better suited for high- adaptability, simplicity of use and accessible information and
level sophisticated performance-based applications like search tools before selecting the language for their project. In general,
engines. Rust is simpler to develop complicated systems with C++ may be a better option for those who value flexibility and
than C++ because it has a better "crate" environment and more control over memory flow, whereas Rust may be a better option
memory safety features. Rust’s simplicity of implementation, for those who place a higher priority on safety and simplicity of
which may make code straightforward and simple to manage, is implementation, but as Rust improves as a language, there might
one of its major advantages. For example, when I was making be a day where Rust can overtake C++ completely and become
the program for Counting Sort, I tried to translate the C++ code the head of programming as Rust is mostly criticised for its lack
directly into Rust, I looked around the system library and realised of maturity.
that there was another way to implement the same algorithm
shorter and easier with built-in functions. References
1. Bugden, W., & Alahmar, A. (2022). Rust: The programming
However, as the Rust compiler compels programmers to write language for safety and performance. arXiv preprint
"safe" code, and more debugging and programme modifications arXiv:2206.05503.
result, Rust’s emphasis on safety can occasionally have an 2. MCFALLS, D. (2017). Concurrent algorithms and data
adverse effect on development productivity [1]. Moreover, multi- structures in c++ and rust. Technical report, Stanford University.

Copyright: ©2023 Vincent Ng. This is an open-access article distributed


under the terms of the Creative Commons Attribution License, which
permits unrestricted use, distribution, and reproduction in any medium,
provided the original author and source are credited.
J Math Techniques Comput Math, 2023 https://opastpublishers.com Volume 2 | Issue 6 | 220

You might also like