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:
- Using the Boolean one-line approach
- Using the Ternary expression approach
- Using the
matchon atupleapproach
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?