Why my rust program is so slow?

I started learning rust recently, and I had heard that rust has high performance. So I write a bubble sort program to checkout. But the program runs slowly compared with c.

Rust code:

use chrono::prelude::*;

extern crate chrono;

fn bubble_sort() {
    let mut arr = vec![91, 44, 16, 32, 50, 55, 13, 12, 33, 65, 69, 71, 74, 57, 48, 61, 2, 58, 38, 27, 51, 39, 81, 79, 36, 80, 95, 23, 72, 63, 3, 30, 35, 59, 18, 28, 5, 56, 96, 40, 84, 78, 42, 8, 15, 25, 10, 43, 41, 75, 45, 99, 46, 82, 68, 76, 0, 90, 7, 49, 64, 53, 62, 89, 88, 21, 14, 4, 70, 73, 29, 98, 24, 26, 92, 97, 6, 100, 47, 67, 87, 60, 22, 52, 85, 93, 19, 31, 54, 83, 77, 20, 86, 66, 11, 37, 34, 9, 1, 94, 17];
    let l = arr.len();

    for i in 0..(l - 1) {
        for j in 0..(l - i - 1) {
            if arr[j] > arr[j + 1] {
                let t = arr[j];
                arr[j] = arr[j + 1];
                arr[j + 1] = t;
            }
        }
    }
}

fn main() {
    let start = Local::now().timestamp_millis();
    // println!("{}", start);
    for _r in 0..10000 {
        bubble_sort();
    }
    let end = Local::now().timestamp_millis();
    println!("{}", end - start);
}

C code:

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include<sys/time.h>

void bubble_sort() {
    int arr[] = {91, 44, 16, 32, 50, 55, 13, 12, 33, 65, 69, 71, 74, 57, 48, 61, 2, 58, 38, 27, 51, 39, 81, 79, 36, 80, 95, 23, 72, 63, 3, 30, 35, 59, 18, 28, 5, 56, 96, 40, 84, 78, 42, 8, 15, 25, 10, 43, 41, 75, 45, 99, 46, 82, 68, 76, 0, 90, 7, 49, 64, 53, 62, 89, 88, 21, 14, 4, 70, 73, 29, 98, 24, 26, 92, 97, 6, 100, 47, 67, 87, 60, 22, 52, 85, 93, 19, 31, 54, 83, 77, 20, 86, 66, 11, 37, 34, 9, 1, 94, 17};
    int l = sizeof(arr) / sizeof(arr[0]);
    for (int i = 0; i < l - 1; i++) {
        for (int j = 0; j < l - i - 1; j++) {
            if (arr[j] > arr[j + 1]) {
                int t = arr[j];
                arr[j] = arr[j + 1];
                arr[j + 1] = t;
            }
        }
    }
}
int main() {
    struct timeval tv;
    
    gettimeofday(&tv, NULL);
    int start = tv.tv_sec * 1000 + tv.tv_usec / 1000;
    for (int i = 0; i < 10000; i++) {
        bubble_sort();
    }
    gettimeofday(&tv, NULL);
    int end = tv.tv_sec * 1000 + tv.tv_usec / 1000;
    printf("%d\n", end - start);
}

Rust version runs within 9000ms, while C version runs within 300ms.

Did I make a mistake?

Did you use --release when you ran it? That enables an optimization build of rust. The default is a debug build equivalent to -O0 in c++

cargo run --release

I tried the exact code with optimizations and I get ~21751ns, that is, 21.75 microseconds

As a side note, here's the fastest code in the world.

The Rust 1.46.0 will be stabilized on 8/27 which allows loop within the const fn so you need a beta compiler to compile it until then.

2 Likes

Nice, I run the program by using cargo run --release, it takes just 38ms on my WSL platform.
Thank you very much.

Bonus - this allows us to find an error in the sorting implementation, by adding the loop to check result and seeing that it is optimised to simple abort:

2 Likes

Actually the bug is my hand-desugared for loop. After fixing it the code only conditionally compiles.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.