For Arduino programming, can I get the same performance with Rust as with C++?

SInce Arduinos are configured with C++, would Rust using the embedded-hal dependency run faster or slower for the Arduino compared to C++?

Depends on which kind of Arduino you use. If it's an AVR-based one, I believe the LLVM support for that is still fairly unoptimized, but for ARM-based ones you should be able to easily beat Arduino code (which often requires doing fairly slow things that you won't have to do in Rust).

1 Like

I see, thanks :slight_smile:

Oh wow nice. What about if I am writting a software for a normal 64-bit computer, which one generates more efficient binaries?

For mainstream 64-bit architectures Rust and sound C++ are about comparable in code efficiency, as a number of threads in this forum in the last six months have documented via extensive benchmarking. In theory Rust code can be more efficient, because Rust's tighter requirements on mutability permit the LLVM backend to generate more efficient code than is permissible for normal C++ programs.

Of course the Rust code is also more likely to be race-free in a multi-threaded environment, and probably will require a lot less time nursing a debugger. That's the Rust tradeoff: you will spend more personal time getting the program to compile and a lot less time debugging it once it does compile.

5 Likes

What about with C? I saw this article which shows how Rust has to do so much more system calls just to print out a simple "Hello World" compared to C and Zig

Here is the link to the website:
https://drewdevault.com/2020/01/04/Slow.html

I was wondering if this is true and why does this happen?

1 Like

Some detective work was done for this a while back:

6 Likes

Unless all you need to do is to literally print a bit of text, that benchmark is misleading.

The author (who's known for articles in the style of "man yells at cloud") tries to imply that some languages are "bloated", but number of syscalls in the startup code doesn't tell you that.

Some languages are optimized for doing more than nothing, so they'll pre-initialize environment for faster memory allocation, support for threads, I/O.

The languages that don't initialize most things before main() will probably have to make these syscalls anyway, just lazily when you use given functionality for the first time. That may actually be less efficient, because they'll have to have if !initialized checks all over the place.

11 Likes

These sorts of micro-benchmarks are worse than useless. Likewise, asking whether C++ is faster than Rust (or vice versa) is the wrong question.

Any real application will be doing much more complex tasks than anything you see in benchmarks, and your actual performance will matter more about how components are structured and how you write code (using references to reduce copying, preferring code that can be vectorised by the compiler, interrupts vs polling, etc.) than which language it is written in.

If you use clang, C and C++ will even go through the same compiler backend, LLVM, and will receive roughly the same sets of optimisations. That means equivalent code will perform identically for all intents and purposes (modulo slight differences in how the standard library implements functions/containers or the effect slightly different ordering of operations may have on which optimisations are performed).

6 Likes

If you go by the definition of creating the most minimalistic program to print "Hello world!" on a screen and that's all you ever wanna do in your programming life, you shouldn't even be installing an operating system only to print "Hello world!" and instead write an "operating system" yourself, that does nothing but print "Hello world!" when starting up the computer and nothing else. Reality is more complex than writing "Hello world!". In conclusion, comparing "Hello world!" programs is of purely theoretical interest and practically useless.

1 Like

It depends on:

  • the compiler support of the architecture: not all architectures are equal although I'd assume that if you use LLVM the support is identical(?)
  • compiler optimisations: since Rust has a different memory model than C++ it could use some optimisations that you can't easily do in C++, like "no-alias" but in practice they're not all enabled, e.g. https://github.com/rust-lang/rust/issues/54878 (If anyone knows more about LLVM optimisations I'd be interested in some comparison of what's possible but not yet enabled/implemented.)
  • your code -- if you translate a C/C++ program directly to Rust, Rust might be slower since it might perform runtime checks that C/C++ don't, like array length checks. Using idiomatic Rust code (in this case e.g. using iterators or checking the lengths manually before a tight loop) will help. But it's up to you, not the compiler to write good code.
2 Likes

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.