1

I'm a complete noob in Rust, I just started to learn this. After playing with some basics I wanted to test how fast is Rust std and do a benchmark for fun. In examples I've seen #[bench] used, so I just checked if it's that simple.

But well...

enter image description here

It does not compile:

error: use of unstable library feature 'test': bench is a part of custom test frameworks which are unstable

That makes me ask the question: what is the proper way to benchmark things in Rust? BTW, does cargo bench benchmarks optimized code or debug build, or is there a way to tell it which build I want to benchmark?

I assume the way I tried to use is deprecated and that's not how you do it in 2021. There ARE similar questions, but the answers are pretty old. I don't want to learn old, deprecated ways and trying to forcibly make them work.

There's a cargo command bench, it's doc has the example with #[bench], yet when I just paste the example I get the error. What's going on here?

Harry
  • 4,524
  • 4
  • 42
  • 81
  • 2
    for microbenchmarking, you should take a look at [criterion](https://bheisler.github.io/criterion.rs/book/index.html). the benchmarking can be made on the build configuration of your choice (debug, release,...) – Jerboas86 Nov 15 '21 at 08:19
  • 2
    Note that _unstable_ does not mean _deprecated_, just the contrary. Unstable features are things that are not yet mature enough to be included in the official _stable_ channel. The stable channel has a compatibility promise, so one something is there it is more difficult to change. If you accept the unstability you can use it by installing the _nightly channel_ of the compiler. – rodrigo Nov 15 '21 at 08:33
  • @Netwave Not really, it rather confuses me. Many completely different ways to solve the problem, while here I ask WHICH one is the quickest and most recommended now. – Harry Nov 15 '21 at 08:36
  • 2
    There is no One True way to run benchmarks. It all depends on your requirements. Using the built-in benchmark suite is perfectly fine if you are ok using the nightly compiler. To make your benchmark work, install the nightly compiler with `rustup install nightly`, select it in your project directory using `rustup override set nightly`, add `#![feature(test)]` at the top of your file and run the benchmark with `cargo bench`. – Sven Marnach Nov 15 '21 at 09:19
  • 1
    I think the linked question is actually a duplicate, so I will close this one. StackOverflow is not a good place to ask for opinions and recommendations to pick between different equally valid options – if you want to have this kind of discussion, I recommend asking on https://users.rust-lang.org/ instead. – Sven Marnach Nov 15 '21 at 09:34
  • Regarding your question about whether benchmarks use the `release` or `dev` profile, the answer is neither – it uses the `bench` profile, which is an optimized build. If you want to benchmark a debug build, you can use `cargo build --bench '*'` to build your benchmark in debug mode and then run it manually. (This would make a good question on its own, so feel free to ask it as a separate question.) – Sven Marnach Nov 15 '21 at 09:41
  • Thanks, you really helped a lot! – Harry Nov 15 '21 at 16:13

0 Answers0