DEV Community

Cover image for Speed Test : Python Vs Rust
Bek Brace
Bek Brace

Posted on

Speed Test : Python Vs Rust

I was hesitant to write this article, especially that I know that the comparison between both worlds, i mean bot languages is not fair, but still - out of fun - I had to do it, you know.

Bot programs have one objective: to count till 200 Million, and then displays how much it took to count.
Let me share with you both codes:
Here is my rust code, keep in mind that it's not optimized, and I haven't even used iterators instead of for in loop; but still it did the job pretty well.

use std::time::{Instant};

fn main() {
    let start = Instant::now();

    let count = (0..200_000_000).count();

    let duration = start.elapsed();
    println!("Count: {}", count);
    println!("Time taken: {} seconds", duration.as_secs());
}
Enter fullscreen mode Exit fullscreen mode

Rust tends to offer better performance due to its emphasis on low-level control, memory safety, and zero-cost abstractions. In this scenario, Rust's compiled nature and ability to optimize loops efficiently contribute to faster execution times.

Here is Python ... !

import time

start = time.time()

count = sum(1 for _ in range(200_000_000))

duration = time.time() - start
print("Count:", count)
print("Time taken: {:.6f} seconds".format(duration))
Enter fullscreen mode Exit fullscreen mode

Now, Python being an interpreted language with dynamic typing and higher-level abstractions, generally shows slower performance compared to Rust. While Python offers excellent readability and ease of use, it typically sacrifices raw performance in exchange for these features.

Finally, I will let you test that yourself, or you can watch my 3 minutes video to understand what's happening :)

Top comments (0)