Performance deep dive

Leap
Leap in Rust

In this approach, we'll find out how to most efficiently calculate if a year is a leap year in Rust.

The approaches page lists three idiomatic approaches to this exercise:

  1. Using the Boolean one-line approach
  2. Using the Ternary expression approach
  3. Using the match on a tuple approach

For our performance investigation, we'll also include two other approaches:

Benchmarks

To benchmark the approaches, we wrote a small benchmark application.

test ternary  ... bench:           0 ns/iter (+/- 0)
test one_line ... bench:           0 ns/iter (+/- 0)
test match    ... bench:           0 ns/iter (+/- 0)
test time     ... bench:          30 ns/iter (+/- 9)
test chrono   ... bench:          18 ns/iter (+/- 3)
test naive    ... bench:          18 ns/iter (+/- 1)

The three main approaches were identical in measurable performance. Of the two date addition approaches, the chrono crate delivered a solution that was 40% faster than the time crate.

28th Aug 2024 · Found it useful?