Better Programming

Advice for programmers.

Follow publication

What Happens If We Code the Same Algorithm in Python and Rust?

From Pythonic to Rustacean — is this the way?

Ángel Berhó
Better Programming
Published in
6 min readMar 5, 2022
Photo by Nick Fewings on Unsplash

Hi again, dear readers! After a long time, I come here to write a post about my first experience with Rust.

Perhaps you are asking yourself (or not) why about this post. Some weeks ago, I read a post from AWS where they talked and display a graphic with all the language codes comparing the Sustainability of all of them. Link to post. In that article, they talked about how they scaled up his platform and how good was to do it with rust. At that moment I was “Wow, really?”. Some days later I read a Twitter thread where the author argues about this post is not saying all the truth. But I’m not here to talk about that. Anyway, if you want to read it too, here is the link.

I‘m a very curious person and a very geek about this topic, so I just started to look up information about Rust. Like the first steps to get started, some GitHub repositories to see how it is the language and I liked it!

What is Rust?

From his page you can get a definition like “A language empowering everyone
to build reliable and efficient software.
” but what is really mean?

Well, Rust is a Compiled language code, this makes our code transforms into machine language, and we could say that our whole process runs are executed as if the hardware get our app and runs it, this should be very fast.

Rust was created in 2010 by Mozilla, ten years later was created Rust foundation to maintain this language as an open-source language like Python and it's a very popular language by stack overflow in their surveys for example this in 2015.

What are the main features? when they developed this language they have in their minds security, When the compiler is called this inspects your code to check if there are any memory bugs or bad pointer references.

Apps you can build

If you think of any language code you probably relate with any type of app, for example, Swift for iOS mobile apps, Python for Web servers and ML, JS for web development, or Java for 3 billion devices.

But nobody will say JS can make web servers or Python can’t scrape webs because probably someone does. And that’s why Rust can do a lot of types of apps: CLI, web server, web assembly (some browsers can execute rust code), Scraping, Microcontrollers (embedded devices). Best of all, it is always with the best performance and low resources compared with other languages codes.

Motivation

When I was a student I started with C and Java, but when I met Python, I fell in love. I think is a very good language to start with, it has a great community and it has a lot of powerful libraries which you can learn a lot of how is the software and understand it.

Finally, when you get the experience and you think you are ready to step up, you can play in the league of compiler languages because you come with design patterns, know how the software works and you only have to add a new variable more, the memory control.

It’s like a car racing videogame, you can’t start with the max difficulty, you start in the low or medium-level and grow up when you get control. This is very similar.

Python vs. Rust: Through a Fibonacci Algorithm

As I said before, Python is a great language to start learning to code. It’s an interpreted language, this script needs python on any device that we want to run it. It’s weakly typed and has a very cool syntax called Pythonic.

Against Rust, we can see the next diagram where Python is executed in any device that has Python installed and Rust just needs to compile the script to translate it into machine language and finally, execute it on any device because the machine can understand it.

Python gives us fast development but it has a big con, his execution is expensive and lower. If we compare the same Fibonacci algorithm we can see a big difference between times.

To see it in a practical way, I coded the same algorithm into Python and Rust.

At first, if we take a look at the Python code, we can see that the lines of code are inferior to Rust. This is because the Python core developers design python to be legible. They did a great job.

In Rust code, we close duplicate the lines. Rust divides code blocks with curly braces while in python the blocks are divided by tabs and spaces. Also, we had to import some libraries to manage big numbers, io, etc.

Python interpreter manages the memory by default but in Rust, you must define which type of variable is it since the max static type of int is u128 (2¹²⁸ bytes = 3.4028237e+38) or usize type which the compiler gets it dynamically from the hardware. So that’s why I implemented BigUint. This library saves in a bytes Vector and allocates memory dynamically.

Let’s talk about performance.

Here, is where Rust domain the panorama.

First, before executing our Rust script, we must compile it for our machine to understand what instructions need to execute.

There are 2 ways for that, with rustc <script.rs> or with cargo, a tool that manages packages, compile, format, and more. We can do it with cargo build [ — release]. If the release flag is added, it will optimize the script and it going to execute the code faster than otherwise.

Calculate the 100th Fibonacci number

After executing with the 100th Fibonacci number we can notice that:

Rust was executed in 26 µs and Python in 142 µs.

Obviously, the performance of these two languages cannot be compared. Remember, we didn’t control the memory, the interpreter did for us. This doesn’t mean that Python core developers designed it badly. It is because the code is read first by the interpreter and it checks errors in execution.

We can put a simple say as if you have to talk to anyone. If both of you understand the language, you will communicate fast. On the other hand, if you need an interpreter to communicate with others, there are more delays in the flow.

My Experience

I can say Rust is cool, after a big part of the time trying to get the easy Fibonacci solution, I can say the change from Python could be a little hard If you are not used to typing variables, managing memory, pointers, and also all the libraries aren’t standard so you have to look for, try it and continue with the development.

On the other hand, although it can be a little difficult to start it, Rust has a great web book where that introduce you to the language with practical examples with a detailed explanation.

With a Fibonacci problem, I have been able to verify precisely that the hardware resources we use are not infinite and that python abstracts us from many problems. I let you some resources that could be interesting.

In conclusion, if you are a curious person that likes to learn something new there is a topic with a lot of stuff to look for and where you can join or if you were waiting for any signal to start something new this is your post.

Thanks for reading, I hope you like it! I’d love to hear your opinion about your language code knowledge or your new learnings.

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

Ángel Berhó
Ángel Berhó

Written by Ángel Berhó

I like to discover new technologies and share with others. #Python #Js #Rust I’m software Engineer @ World

Responses (8)

Write a response