-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfpdec_comparison.rs
155 lines (140 loc) · 5.93 KB
/
fpdec_comparison.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
//! Compare `const-decimal` with [`fpdec.rs`](https://github.com/mamrhein/fpdec.rs)
use criterion::{black_box, criterion_group, criterion_main, Criterion};
use fpdec::{Dec, Decimal};
// Lots of black boxes, maybe overkill.
fn criterion_benchmark(c: &mut Criterion) {
// Create instances here to avoid benchmarking the constructor.
let fpdec_0 = fpdec::Decimal::new_raw(100, 0);
let fpdec_1 = fpdec::Decimal::new_raw(110, 0);
let const_decimal_i32_0 = const_decimal::Decimal::<i32, 4>::try_from_scaled(100, 0).unwrap();
let const_decimal_i32_1 = const_decimal::Decimal::<i32, 4>::try_from_scaled(110, 0).unwrap();
let const_decimal_i64_0 = const_decimal::Decimal::<i64, 4>::try_from_scaled(100, 0).unwrap();
let const_decimal_i64_1 = const_decimal::Decimal::<i64, 4>::try_from_scaled(110, 0).unwrap();
c.bench_function(&format!("fpdec_add"), |b| {
b.iter(|| {
let _ = black_box(black_box(fpdec_0) + black_box(fpdec_1));
})
});
c.bench_function(&format!("const_decimal_i32_add"), |b| {
b.iter(|| {
black_box(black_box(const_decimal_i32_0) + black_box(const_decimal_i32_1));
})
});
c.bench_function(&format!("const_decimal_i64_add"), |b| {
b.iter(|| {
black_box(black_box(const_decimal_i64_0) + black_box(const_decimal_i64_1));
})
});
c.bench_function(&format!("fpdec_sub"), |b| {
b.iter(|| {
let _ = black_box(black_box(fpdec_0) - black_box(fpdec_1));
})
});
c.bench_function(&format!("const_decimal_i32_sub"), |b| {
b.iter(|| {
black_box(black_box(const_decimal_i32_0) - black_box(const_decimal_i32_1));
})
});
c.bench_function(&format!("const_decimal_i64_sub"), |b| {
b.iter(|| {
black_box(black_box(const_decimal_i64_0) - black_box(const_decimal_i64_1));
})
});
c.bench_function(&format!("fpdec_mul"), |b| {
b.iter(|| {
let _ = black_box(black_box(fpdec_0) * black_box(fpdec_1));
})
});
c.bench_function(&format!("const_decimal_i32_mul"), |b| {
b.iter(|| {
black_box(black_box(const_decimal_i32_0) * black_box(const_decimal_i32_1));
})
});
c.bench_function(&format!("const_decimal_i64_mul"), |b| {
b.iter(|| {
black_box(black_box(const_decimal_i64_0) * black_box(const_decimal_i64_1));
})
});
c.bench_function(&format!("fpdec_div"), |b| {
b.iter(|| {
let _ = black_box(black_box(fpdec_0) / black_box(fpdec_1));
})
});
c.bench_function(&format!("const_decimal_i32_div"), |b| {
b.iter(|| {
black_box(black_box(const_decimal_i32_0) / black_box(const_decimal_i32_1));
})
});
c.bench_function(&format!("const_decimal_i64_div"), |b| {
b.iter(|| {
black_box(black_box(const_decimal_i64_0) / black_box(const_decimal_i64_1));
})
});
// Some real-world use case examples. Linear futures profit and loss
// calculations. Quantity denoted in BaseCurrency.
let fpdec_entry_price = Dec!(100);
let fpdec_exit_price = Dec!(110);
let fpdec_qty = Dec!(5);
let const_decimal_i32_entry_price =
const_decimal::Decimal::<i32, 2>::try_from_scaled(100, 0).unwrap();
let const_decimal_i32_exit_price =
const_decimal::Decimal::<i32, 2>::try_from_scaled(110, 0).unwrap();
let const_decimal_i32_qty = const_decimal::Decimal::<i32, 2>::try_from_scaled(5, 0).unwrap();
let const_decimal_i64_entry_price =
const_decimal::Decimal::<i64, 2>::try_from_scaled(100, 0).unwrap();
let const_decimal_i64_exit_price =
const_decimal::Decimal::<i64, 2>::try_from_scaled(110, 0).unwrap();
let const_decimal_i64_qty = const_decimal::Decimal::<i64, 2>::try_from_scaled(5, 0).unwrap();
c.bench_function(&format!("fpdec_linear_futures_pnl"), |b| {
b.iter(|| {
let _ = black_box(fpdec_exit_price * fpdec_qty - fpdec_entry_price * fpdec_qty);
})
});
c.bench_function(&format!("const_decimal_i32_linear_futures_pnl"), |b| {
b.iter(|| {
black_box(
const_decimal_i32_exit_price * const_decimal_i32_qty
- const_decimal_i32_entry_price * const_decimal_i32_qty,
);
})
});
c.bench_function(&format!("const_decimal_i64_linear_futures_pnl"), |b| {
b.iter(|| {
black_box(
const_decimal_i64_exit_price * const_decimal_i64_qty
- const_decimal_i64_entry_price * const_decimal_i64_qty,
);
})
});
// Inverse futures profit and loss calculation. Quantity is denoted in
// QuoteCurrency.
let fpdec_qty = Dec!(500);
let const_decimal_i32_qty = const_decimal::Decimal::<i32, 2>::try_from_scaled(500, 0).unwrap();
let const_decimal_i64_qty = const_decimal::Decimal::<i64, 2>::try_from_scaled(500, 0).unwrap();
c.bench_function(&format!("fpdec_inverse_futures_pnl"), |b| {
b.iter(|| {
let _ = black_box(
black_box(fpdec_qty) / black_box(fpdec_entry_price)
- black_box(fpdec_qty) / black_box(fpdec_exit_price),
);
})
});
c.bench_function(&format!("const_decimal_i32_inverse_futures_pnl"), |b| {
b.iter(|| {
black_box(
black_box(const_decimal_i32_qty) / black_box(const_decimal_i32_entry_price)
- black_box(const_decimal_i32_qty) / black_box(const_decimal_i32_exit_price),
);
})
});
c.bench_function(&format!("const_decimal_i64_inverse_futures_pnl"), |b| {
b.iter(|| {
black_box(
black_box(const_decimal_i64_qty) / black_box(const_decimal_i64_entry_price)
- black_box(const_decimal_i64_qty) / black_box(const_decimal_i64_exit_price),
);
})
});
}
criterion_group!(benches, criterion_benchmark);
criterion_main!(benches);