You are on page 1of 9

Budapest University of Technology and Economics

“The Memory Efficiency of


Rust”
Eldar Maharramli
Utkarsh Kumar
Samirawit Fetene

Professor:
Tamás Kovácsházy
BUDAPEST, 2023
Project Description
The goal of this project is to learn about memory efficiency properties of Rust
programming language by making comparisons of the programs that are written
in Rust and Python. Raspberry Pi 2 will be used to run programs, because we
want to test our project in a real hardware. To check the efficiency, execution
time and memory capacity will be considered in this project.

Figure 1. Structure of PC-Raspberry Pi Connection


In order to avoid the need for external monitor for Raspberry Pi, we plan to use
SSH connection over Ethernet. In this way, we can connect the Raspberry Pi to
regular laptop and test our Rust & Python programs. To illustrate, the runtime
efficiency of these can be tested: 1) Standard library functions, like print(),
input(), range(), str(), etc. ; 2) Standard mathematical functions, like abs(),
pow(), round(), random(), etc. ; 3) Simple algorithms, like calculating the value
of Pi and sorting algorithms.
Software Used:
Putty: A terminal emulator software used to establish SSH connections to
remote servers or devices.
Htop or Sar: A command-line utility that provides a real-time view of resource
usage on Linux-based systems.
Vim, Nano: text editors commonly used in Unix-based systems for creating and
modifying files through the command line.
Thonny: Integrated development environment (IDE) for programming in
Python, featuring a simplified interface and helpful debugging tools.
Hardware used:
1) Laptop PC
2) Raspberry Pi
3) Ethernet Cable
4) Power Supply 5V 2A

Introduction about Rust


Rust is a modern systems programming language that was initially developed by Mozilla. It
combines low-level control over hardware resources with high-level abstractions, making it a
powerful and safe language for building efficient and reliable software. Rust's design goals
revolve around three key principles: safety, concurrency, and performance.
One of the standout features of Rust is its emphasis on memory safety. It enforces strict
compile-time checks to prevent common programming errors like null pointer dereferences,
buffer overflows, and data races. By employing a unique ownership system and borrowing
rules, Rust ensures that memory is managed correctly without the need for garbage
collection, resulting in fast and efficient code.
Concurrency is another core aspect of Rust. It provides built-in support for writing concurrent
and parallel programs through lightweight threads called "async tasks." Rust's ownership
model allows for safe sharing of data between tasks, eliminating common concurrency bugs
like data races. Additionally, Rust's "fearless concurrency" approach enables developers to
write highly performant and scalable code while maintaining safety.
Rust is known for its impressive performance characteristics. It offers fine-grained control
over system resources, allowing developers to optimize their code for speed and efficiency.
The language's zero-cost abstractions and minimal runtime overhead make it suitable for a
wide range of applications, from embedded systems to web servers.
Furthermore, Rust boasts a vibrant and growing ecosystem of libraries and frameworks,
making it easier for developers to build complex applications. It has gained popularity in
areas such as systems programming, web development, network services, and even game
development.

Difference between Rust and Phyton

Rust and Python are two programming languages that have distinct differences
in terms of their design philosophies, performance characteristics, and areas of
application. Here are some key differences between Rust and Python:
1. Performance: - Rust is a statically typed, compiled language that emphasizes
performance and memory safety. It achieves this through features like strict
ownership and borrowing rules, which enable efficient memory management
and eliminate common bugs like null pointer dereferences and data races.
Python, on the other hand, is an interpreted language known for its simplicity
and ease of use but generally has a slower execution speed compared to Rust.
2. Type System: Rust has a strong static type of system that enforces strict
compile-time type checking. It uses type inference to determine types wherever
possible but requires explicit type annotations in certain cases. Python, on the
other hand, is dynamically typed, which means that variable types are
determined at runtime. Python allows for flexible and dynamic programming
but can lead to certain types of runtime errors that would be caught during
compilation in Rust.
3. Concurrency and Parallelism: Rust provides powerful features for safe
concurrency and parallelism. Its ownership and borrowing system allow for
fine-grained control over mutable data access, making it easier to write
concurrent and thread-safe code. Python, on the other hand, has a Global
Interpreter Lock (GIL) that prevents true parallel execution of threads, limiting
its ability to fully utilize multi-core processors for CPU-bound tasks. However,
Python offers high-level abstractions and libraries for asynchronous
programming, making it suitable for I/O-bound applications.
4. Ecosystem and Libraries: - Python has a vast and mature ecosystem with a
wide range of libraries and frameworks for various domains, including web
development, data analysis, machine learning, and more. Rust's ecosystem is
growing rapidly, but it is still relatively young compared to Python. Rust excels
in areas where high-performance and system-level programming are required,
such as network programming, systems development, and low-level hardware
interactions.

5. Ease of Use: Python is often praised for its simplicity and readability. Its
syntax is designed to be human-friendly, making it easy to write and understand
code. Rust, while it has a steeper learning curve, provides advanced features
and expressive syntax that gives developers fine-grained control and enables
writing highly performant and safe code.
Methodology
To start the PuTTY connection, these settings must be configured:
Figure 2. Connecting To raspberry Pi with windows laptop

Figure 3. Running Python Programs in Command Line


In order to run Rust Programs in Raspberry Pi, we need the following command:
rustc pi.rs && ./pi.rs
To monitor CPU and Memory usage, SAR is used. In the figure 3, we can see the
initialization of two ssh windows, which one runs the Sar, and in the right window, we test
our programs.
Figure 4. Running Task Manager in Raspberry pi

Results
1) Calculating the Value of Pi
In order to calculate the value of Pi, pi.py and py.rs files are used. The graph below
illustrates CPU Power occupation in Y axis and execution time in X axis. From the
graph, we can see that, Rust is 86% efficient in terms of CPU power occupation.

2) Summation of Values from 0 to 10.000.000


In order to calculate the summation, summation.py and summation.rs files are used. In
this case Rust was 33.33% faster than Python:
3) Finding Prime numbers from 0 to 10.000.000
In order to find the prime numbers from 0 to 10.000.00, primes.py and primes.rs files
are used. In this case Rust was 82.14% faster than Python:

4) Matrix Multiplications
In order to do matrix operations, matrix.py and matrix.rs files are used. In this case Rust
was 84.79% faster than Python:

In the below figure, we can see some standard library functions and how much
execution time they occupy.
Figure 5. SDL Functions and their execution time in Raspberry Pi.

You might also like