O'Reilly logo

Mastering Rust by Vesa Kaihlavirta

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Benchmarks

Just one more thing and we're done with the lecture portion. Benchmark tests allow us to bring automation to yet another boring and repetitive task: measuring the speed of our code. This is supported by two things:

  • The #[bench] annotation marks a function as a benchmark
  • The standard library module test has a Bencher type, which the benchmark function uses for benchmark iterations

Unfortunately, benchmark tests are an unstable feature, so we will have to use the nightly compiler for the following. Fortunately, with rustup, moving between different versions with the rustup compiler is easy. First, we'll make sure that the nightly compiler is installed:

OK, now we can write and run a simple benchmark test. Benchmarks require Cargo, ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required