-
Notifications
You must be signed in to change notification settings - Fork 520
/
main.rs
76 lines (61 loc) · 1.53 KB
/
main.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#![feature(test)]
extern crate test;
use test::Bencher;
fn main() {
println!("Hello, world!");
}
pub fn is_leap_year(year: u64) -> bool {
//exhausts after only two checks
if year % 100 == 0 {
year % 400 == 0
} else {
year % 4 == 0
}
}
pub fn is_leap_year_one_line(year: u64) -> bool {
(year % 4 == 0) && ((year % 100 != 0) || (year % 400 == 0))
}
pub fn is_leap_year_match(year: u64) -> bool {
match (year % 4, year % 100, year % 400) {
(_, _, 0) => true,
(_, 0, _) => false,
(0, _, _) => true,
_ => false,
}
}
use time::{Date, Duration, Month};
pub fn is_leap_year_time(year: u64) -> bool {
(Date::from_calendar_date(year as i32, Month::February, 28).unwrap() + Duration::DAY).day()
== 29
}
use chrono::prelude::*;
pub fn is_leap_year_chrono(year: u64) -> bool {
(Utc.ymd(year as i32, 2, 28) + chrono::Duration::days(1)).day() == 29
}
pub fn is_leap_year_naive(year: u64) -> bool {
(NaiveDate::from_ymd(year as i32, 2, 28) + chrono::Duration::days(1)).day() == 29
}
#[bench]
fn ternary(b: &mut Bencher) {
b.iter(|| is_leap_year(2000));
}
#[bench]
fn one_line(b: &mut Bencher) {
b.iter(|| is_leap_year_one_line(2000));
}
#[bench]
fn match(b: &mut Bencher) {
b.iter(|| is_leap_year_match(2000));
}
#[bench]
fn time(b: &mut Bencher) {
b.iter(|| is_leap_year_time(2000));
}
#[bench]
fn chrono(b: &mut Bencher) {
b.iter(|| is_leap_year_chrono(2000));
}
#[bench]
fn naive(b: &mut Bencher) {
b.iter(|| is_leap_year_naive(2000));
}