Which is faster: Rust or C? Let’s find out who is the Usain Bolt of programming world.

Rushabh Gaherwar
Level Up Coding
Published in
6 min readNov 3, 2020

A comparative study on the speed of C & Rust language using popular sorting algorithms.

C, the ninja turtle’s master

“C is quirky, flawed, and an enormous success.”

Dennis Ritchie

C, one of the most established programming languages, is an imperative computer programming language for general purposes that dates back to 1969. Dennis Ritchie is the father of the C programming language. In 1989, the American National Standards Institute and the International Organisation for Standardisation developed new consensus standards for C. It’s still a versatile and widely used programming language, built as a simple, low-level programming language that works across platforms.

“C++ and Java, say, are presumably growing faster than plain C, but I bet C will still be around.”

Dennis Ritchie

Popular household devices such as microwave ovens, washing machines and digital cameras are becoming more sophisticated by the day. A microprocessor, an operating system and software contained in these machines provide this knowledge. Not only do these programs have to run efficiently, but they still have to work on a small amount of memory. It is not shocking that such programs are written in C.

Major parts of popular operating systems like Windows, UNIX, Linux is still written in C. This is because even today when it comes to performance (speed of execution) nothing beats C (but not from now onwards). Moreover, if one is to extend the operating system to work with new devices one needs to write device driver programs. These programs are exclusively written in C.

C language has a huge impact on the IT industry and still playing a vital role.

Rust, the potential contender in every field

Rust is a multi-paradigm programming language focused on performance and safety, especially safe concurrency. Rust is syntactically similar to C++, and provides memory safety without using garbage collection, but instead through the use of a borrow checking system.

Rust was originally designed by Graydon Hoare at Mozilla Research.

*rust

Rust basically focus on the following things:-

1. Concurrency.

2. Safety.

3. Speed.

Memory safety

A main selling point of Rust is that it guarantees, at compile-time, that your application will be safe from dereferencing null or dangling pointers. Rust also makes it difficult to leak memory.

Performance

Rust language does not have a Garbage Collector (GC) by design. Rust would know when the variable gets out of scope or its lifetime ends at compile time and thus insert the corresponding LLVM/assembly instructions to free the memory. This improves the performance at runtime.

This is how Rust works with memory.

Multi-threaded

In Rust, threads are “isolated” from each other automatically, due to ownership. Writes can only happen when the thread has mutable access, either by owning the data or by having a mutable borrow of it. Either way, the thread is guaranteed to be the only one with access at the time

Support for Web Assembly

Web Assembly helps to execute high computation-intensive algorithms in the browser, on embedded devices, or anywhere else. It runs at the speed of native code. Rust can be compiled to Web Assembly for fast, reliable execution.

Comparison between the speed of C & Rust

Methodology

The approach to compare the speed of these two languages is by writing the most popular sorting algorithms and running them for different element ranges and comparing which language took the longest time in sorting the elements. The sorting algorithm which I’m using is:-

1. Bubble Sort.

2. Insertion Sort.

3. Selection Sort.

4. Shell Sort.

5. Heap Sort.

*Time complexity of popular sorting algorithms

Above is the time complexity of popular sorting algorithms, by looking at the worst-case complexity, we can have a rough idea of which sorting technique would perform the worst among themselves.

Bubble sort has the worst-case complexity of O(N²) and certainly will perform the worst as compared to the other sorting techniques. The resulting matrix that we would be generating will also help us to understand the worst-case complexity in terms of time by comparing the time required to sort the elements.

So, I have written all the above 5 algorithms both in C & Rust. For the input purpose, I’m using an array of integers and populating that array with random numbers using a random-number-generator function rand() in C and rand::Rng in Rust. This array is given as input to the sorting algorithms and output is recorded in a matrix. Testing the performance of the algorithms for three ranges 1000, 10000 & 100000 elements.

This setup has been running for 100 iterations and averaging is done to generate the output matrix.

Setup for Performance Comparison

The System configuration on which the above setup has been done and ran is:-

macOS Catalina

MacBook Pro (16-inch, 2019)

Processor:- 2.3 GHz 8-Core Intel Core i9

Memory:- 16 GB 2667 MHz DDR4

clang version 12.0.0 (optimisation flag -O3)

rustc 1.45.2 (optimisation flag -release)

Performance Stats

Below are the output matrices generated:-

*Performance of C language on different sorting algorithms.
*Performance of Rust language on different sorting algorithms.

It’s very clear from the output matrix that the results are somewhat equal. For each sorting technique and input array range, in some cases Rust performs better and in some cases C performs better. In lower ranges of elements (1000 elements) Rust performs better except in insertion sort. For the range of 10000 elements, C performs better for each sorting technique.

*Graphical representation.

The bar charts also depicts that the results are almost equal in all the cases.

You can try implementing it on your own system.

Github Link of the project:- https://github.com/imrushabh/C_vs_Rust

Conclusion

It’s quite difficult to say which language is faster because it depends on case to case. But we can say that Rust is a competitor of C in terms of speed and it is faster than many other popular languages like Java and Python. Rust provides lots of functionalities like it focuses on speed, memory safety and parallelism and is open source, we can use Rust to create a wide range of new software applications, such as game engines, operating systems, file systems, browser components and simulation engines for virtual reality. We would be seeing Rust everywhere in the coming time for sure!!!

Support me by buying me a coffee :- buymeacoffee.com/rushabh

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

Responses (6)

What are your thoughts?