Writing faster code
Premature optimization is the root of all evil
– Donald Knuth
A good software design tends to create faster programs, while a bad software design tends to create slower programs. If you find yourself asking, "Why is my program slow?, then first ask yourself, Is my program disorderly?"
In this section, we describe some performance tips. These are generally good habits when programming in Rust that will coincidentally lead to improved performance. If your program is slow, then first check to see whether you are violating one of these principles.
Compiling with release mode
This is a really simple suggestion that you should know about if you are at all concerned about performance.
- Rust normally compiles in debug mode, which is slow:
cargo build
- Rust optionally compiles in release mode, which is fast:
cargo build --release
- Here is a comparison using debug mode for a toy...