Performance of Rust versus C

I am a beginner of Rust.

Rust is described as "fast and memory-efficient" of its performance, but why the exe file seems not like this?

#main.c

#include "stdio.h"
int main() {
    printf("Hello, world!\n");
    return 0;
}

#src/main.rs

fn main() {
    println!("Hello, world!");
}

My compiler is MinGW-gcc. The size of the exe file compiled from main.c is 53k, but that from main.rs is 3474k in release mode. How should I look at the performance of Rust versus C?

Benchmark and measure (not the file size, but working memory etc).

For explanation about the file size difference look up dynamic vs static linking.

3 Likes

I don't think binary size is usually included when considering performance, though it can of course be a relevant metric to consider in some contexts. The bulk of the difference is due to Rust binaries being statically linked, I believe. This blog post is a few years old, but still relevant I think: https://lifthrasiir.github.io/rustlog/why-is-a-rust-executable-large.html

4 Likes

If binary size is a concern, I recommend you acquaint yourself with this github repo: https://github.com/johnthagen/min-sized-rust.

3 Likes

If you want to have small binaries you can give UPX(https://upx.github.io/) a chance. But please be careful if execution performance and RAM usage. But it creates small binaries with a cost of higher starting time and its using more RAM.

1 Like

Thanks for all replies. I think your answer @Heliozoa is exactly what I want, thanks again.

1 Like