-
-
Notifications
You must be signed in to change notification settings - Fork 256
/
instant.rs
93 lines (79 loc) · 2.35 KB
/
instant.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
#![allow(deprecated)]
use std::time::Instant as StdInstant;
use criterion::Bencher;
use time::ext::NumericalDuration;
use time::{Duration, Instant};
setup_benchmark! {
"Instant",
// region: checked arithmetic
fn checked_add(ben: &mut Bencher<'_>) {
let instant = Instant::now();
let dt = 5.seconds();
ben.iter(|| instant.checked_add(dt));
}
fn checked_sub(ben: &mut Bencher<'_>) {
let instant = Instant::now();
let dt = 5.seconds();
ben.iter(|| instant.checked_sub(dt));
}
// endregion checked arithmetic
// region: trait impls
fn sub(ben: &mut Bencher<'_>) {
let start: Instant = Instant::now();
let end: Instant = start + 1.milliseconds();
ben.iter(|| end - start);
}
fn add_duration(ben: &mut Bencher<'_>) {
let start = Instant::now();
let dt: Duration = 1.seconds();
ben.iter(|| start + dt);
}
fn std_add_duration(ben: &mut Bencher<'_>) {
let start = StdInstant::now();
let dt: Duration = 1.milliseconds();
ben.iter(|| start + dt);
}
fn add_assign_duration(ben: &mut Bencher<'_>) {
let dt: Duration = 1.milliseconds();
iter_batched_ref!(
ben,
Instant::now,
[|start| *start += dt]
);
}
fn std_add_assign_duration(ben: &mut Bencher<'_>) {
let dt: Duration = 1.milliseconds();
iter_batched_ref!(
ben,
StdInstant::now,
[|start| *start += dt]
);
}
fn sub_duration(ben: &mut Bencher<'_>) {
let instant = Instant::now();
let dt: Duration = 100.milliseconds();
ben.iter(|| instant - dt);
}
fn std_sub_duration(ben: &mut Bencher<'_>) {
let instant = StdInstant::now();
let dt: Duration = 100.milliseconds();
ben.iter(|| instant - dt);
}
fn sub_assign_duration(ben: &mut Bencher<'_>) {
let dt: Duration = 100.milliseconds();
iter_batched_ref!(
ben,
Instant::now,
[|instant| *instant -= dt]
);
}
fn std_sub_assign_duration(ben: &mut Bencher<'_>) {
let dt: Duration = 100.milliseconds();
iter_batched_ref!(
ben,
StdInstant::now,
[|instant| *instant -= dt]
);
}
// endregion trait impls
}