forked from FluenTech/embedded-time
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathduration.rs
122 lines (106 loc) · 3.46 KB
/
duration.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
use criterion::{black_box, criterion_group, criterion_main, BenchmarkId, Criterion};
use embedded_time::{duration::*, rate::*};
use std::convert::TryFrom;
use std::mem::size_of;
fn duration_vs_core_duration(c: &mut Criterion) {
use core::time::Duration;
let mut group = c.benchmark_group("Duration Construct/Read");
println!(
"embedded_time::Duration size: {} B",
size_of::<Milliseconds<u64>>()
);
group.bench_function("embedded_time::Duration", |b| {
b.iter(|| {
let ms = black_box(123);
let duration = Milliseconds::<u64>(ms);
let count = duration.integer();
let _ = black_box(count);
})
});
println!("core::time::Duration size: {} B", size_of::<Duration>());
group.bench_function("core::time::Duration", |b| {
b.iter(|| {
let ms = black_box(123);
let core_duration = Duration::from_millis(ms);
let count = core_duration.as_millis();
let _ = black_box(count);
})
});
group.finish();
}
fn conversions(c: &mut Criterion) {
let mut group = c.benchmark_group("conversions");
let rate = 500_u32.Hz();
group.bench_with_input(
BenchmarkId::new("Hertz<u32>::to_duration::<Milliseconds<u32>>()", rate),
&rate,
|b, &_size| {
b.iter(|| rate.to_duration::<Milliseconds<u32>>());
},
);
let duration = 500_u32.seconds();
group.bench_with_input(
BenchmarkId::new("Nanoseconds<u32>::try_from::<Seconds<u32>>()", duration),
&duration,
|b, &_size| {
b.iter(|| Nanoseconds::<u32>::try_from(duration));
},
);
let duration = 500_u32.milliseconds();
group.bench_with_input(
BenchmarkId::new(
"Nanoseconds<u32>::try_from::<Milliseconds<u32>>()",
duration,
),
&duration,
|b, &_size| {
b.iter(|| Nanoseconds::<u32>::try_from(duration));
},
);
let duration = 500_u64.nanoseconds();
group.bench_with_input(
BenchmarkId::new("Seconds<u32>::try_from::<Nanoseconds<u64>>()", duration),
&duration,
|b, &_size| {
b.iter(|| Seconds::<u32>::try_from(duration));
},
);
let duration = 500_u64.nanoseconds();
group.bench_with_input(
BenchmarkId::new(
"Seconds<u32>::try_from::<Nanoseconds<u64>>() (unwrap)",
duration,
),
&duration,
|b, &_size| {
b.iter(|| Seconds::<u32>::try_from(duration).unwrap());
},
);
let duration = 500_u32.nanoseconds();
group.bench_with_input(
BenchmarkId::new("Seconds<u64>::from::<Nanoseconds<u32>>()", duration),
&duration,
|b, &_size| {
b.iter(|| Seconds::<u64>::from(duration));
},
);
let duration = 500_u32.nanoseconds();
group.bench_with_input(
BenchmarkId::new("Seconds<u32>::from::<Nanoseconds<u32>>()", duration),
&duration,
|b, &_size| {
b.iter(|| Seconds::<u32>::from(duration));
},
);
let duration = 500_u32.nanoseconds();
group.bench_with_input(
BenchmarkId::new("Milliseconds<u32>::from::<Nanoseconds<u32>>()", duration),
&duration,
|b, &_size| {
b.iter(|| Milliseconds::<u32>::from(duration));
},
);
group.finish();
}
criterion_group!(benches, duration_vs_core_duration, conversions);
criterion_main!(benches);