500M Speed Test : C Rust

Rmag Breaking News

Hey guys, it’s Amir from Bek Brace yet again with another speed test between C and Rust programming languages, When it comes to programming languages, there’s always been a bit of a rivalry between Rust and C. Each has its die-hard fans swearing by its strengths. But what happens when you pit them against each other in a speed test? Well, let me tell you, the results might just blow your mind – if you want to skip the reading, go directly to the test video, it’s only a few min to see Rust in both optimized and unoptimized versions of the code.

Setting the Scene

So, we set up this experiment to see which one could handle a heavy-duty task better. We’re talking about counting from 0 to almost half a billion. Pretty hefty, right? We had two setups for Rust: one using just one thread and the other spreading the load across four threads to take advantage of multicore processors. And of course, we had a C version doing the same thing.

First up, Rust.
Now, the single-threaded Rust version? Smooth as silk. It zipped through the task without breaking a sweat. Here’s a snippet of the code:

// Rust single-threaded unoptimized code
use std::time::{Instant};

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

let count = (0..500_000_000).fold(0,|acc,_| acc + 1);

let duration = start.elapsed();

println!(“Count in Rust: {}”, count);
println!(“Time Taken: {:.2} seconds”, duration.as_secs_f64());
}

But where Rust really flexed its muscles was with its multi-threaded setup. It handled the workload like a champ, splitting it across four threads and getting the job done in record time. Check out this piece of code:

// Rust multi-threaded optimized code
use std::time::{Instant};
use std::thread;

fn main(){
let start = Instant::now();
let num_threads = 4;
let chunk_size = 500_000_000 / num_threads;
let handles: Vec<_> = (0..num_threads).map(|i| {
let start = i * chunk_size;
let end = if i == num_threads 1 {
500_000_000
}else{
start + chunk_size
};
thread::spawn(move || {
let count = (start..end).fold(0, |acc, _| acc + 1);
count
})
}).collect();

let mut total_count = 0;
for handle in handles {
total_count += handle.join().unwrap();
}
let duration = start.elapsed();

println!(“Count in Rust: {}”, total_count);
println!(“Time taken: {:.2} seconds”, duration.as_secs_f64());
}

Actually, some of my friends asked me to change the fold method, and here’s his comment on the video:
Try use in the single-thread version of the test this code instead of “fold”:

let mut count = 0; (0..500_000_000).map(|_| count += 1).max();

Try it, and let me know if this changes something.

Now, let’s talk about C. To be honest, we didn’t expect much. But boy, were we in for a surprise! The C version not only kept up with Rust but actually outperformed it by a mile! Here’s a snippet of the C code (Good I kept them undeleted as I haven’t pushed them to GitHub)

#include <stdio.h>
#include
<time.h>

int main() {
clock_t start = clock();
long count = 0;
for (long i = 0; i < 500000000; ++i) {
count++;
}
clock_t end = clock();
double time_taken = ((double)(end start)) / CLOCKS_PER_SEC;
printf(“Count in C: %ldn, count);
printf(“Time taken: %.2f secondsn, time_taken);
return 0;
}

And get this – no crashes [0.29 second]!
It powered through the task without a hiccup, leaving us scratching our heads in amazement.

So, what did we learn from this showdown?
Well, Rust certainly put up a good fight, especially with its multi-threading mojo. But when it comes to sheer speed and reliability, C takes the cake!!
Its performance was rock solid, leaving us with a newfound respect for this old-school language.

But hey, the beauty of it all is that both languages have their strengths. Rust may have lost this round, but who knows what the future holds? One thing’s for sure – the world of programming is full of surprises, and we can’t wait to see what comes next!

Thank you for reading / and watching – I’ll see you in the next one.

Leave a Reply

Your email address will not be published. Required fields are marked *