| 1 |
#![feature(test)] |
| 2 |
|
| 3 |
extern crate test; |
| 4 |
|
| 5 |
use bincode::Options as _; |
| 6 |
use core::str::FromStr; |
| 7 |
use rust_decimal::Decimal; |
| 8 |
|
| 9 |
macro_rules! bench_decimal_op { |
| 10 |
($name:ident, $op:tt, $x:expr, $y:expr) => { |
| 11 |
#[bench] |
| 12 |
fn $name(b: &mut ::test::Bencher) { |
| 13 |
let x = Decimal::from_str($x).unwrap(); |
| 14 |
let y = Decimal::from_str($y).unwrap(); |
| 15 |
b.iter(|| { |
| 16 |
let result = x $op y; |
| 17 |
::test::black_box(result); |
| 18 |
}); |
| 19 |
} |
| 20 |
} |
| 21 |
} |
| 22 |
|
| 23 |
macro_rules! bench_fold_op { |
| 24 |
($name:ident, $op:tt, $init:expr, $count:expr) => { |
| 25 |
#[bench] |
| 26 |
fn $name(b: &mut ::test::Bencher) { |
| 27 |
fn fold(values: &[Decimal]) -> Decimal { |
| 28 |
let mut acc: Decimal = $init.into(); |
| 29 |
for value in values { |
| 30 |
acc = acc $op value; |
| 31 |
} |
| 32 |
acc |
| 33 |
} |
| 34 |
|
| 35 |
let values: Vec<Decimal> = test::black_box((1..$count).map(|i| i.into()).collect()); |
| 36 |
b.iter(|| { |
| 37 |
let result = fold(&values); |
| 38 |
::test::black_box(result); |
| 39 |
}); |
| 40 |
} |
| 41 |
} |
| 42 |
} |
| 43 |
|
| 44 |
/* Add */ |
| 45 |
bench_decimal_op!(add_self, +, "2.01", "2.01"); |
| 46 |
bench_decimal_op!(add_simple, +, "2", "1"); |
| 47 |
bench_decimal_op!(add_one, +, "2.01", "1"); |
| 48 |
bench_decimal_op!(add_two, +, "2.01", "2"); |
| 49 |
bench_decimal_op!(add_one_hundred, +, "2.01", "100"); |
| 50 |
bench_decimal_op!(add_point_zero_one, +, "2.01", "0.01"); |
| 51 |
bench_decimal_op!(add_negative_point_five, +, "2.01", "-0.5"); |
| 52 |
bench_decimal_op!(add_pi, +, "2.01", "3.1415926535897932384626433832"); |
| 53 |
bench_decimal_op!(add_negative_pi, +, "2.01", "-3.1415926535897932384626433832"); |
| 54 |
|
| 55 |
bench_fold_op!(add_10k, +, 0, 10_000); |
| 56 |
|
| 57 |
/* Sub */ |
| 58 |
bench_decimal_op!(sub_self, -, "2.01", "2.01"); |
| 59 |
bench_decimal_op!(sub_simple, -, "2", "1"); |
| 60 |
bench_decimal_op!(sub_one, -, "2.01", "1"); |
| 61 |
bench_decimal_op!(sub_two, -, "2.01", "2"); |
| 62 |
bench_decimal_op!(sub_one_hundred, -, "2.01", "100"); |
| 63 |
bench_decimal_op!(sub_point_zero_one, -, "2.01", "0.01"); |
| 64 |
bench_decimal_op!(sub_negative_point_five, -, "2.01", "-0.5"); |
| 65 |
bench_decimal_op!(sub_pi, -, "2.01", "3.1415926535897932384626433832"); |
| 66 |
bench_decimal_op!(sub_negative_pi, -, "2.01", "-3.1415926535897932384626433832"); |
| 67 |
|
| 68 |
bench_fold_op!(sub_10k, -, 5_000_000, 10_000); |
| 69 |
|
| 70 |
/* Mul */ |
| 71 |
bench_decimal_op!(mul_one, *, "2.01", "1"); |
| 72 |
bench_decimal_op!(mul_two, *, "2.01", "2"); |
| 73 |
bench_decimal_op!(mul_one_hundred, *, "2.01", "100"); |
| 74 |
bench_decimal_op!(mul_point_zero_one, *, "2.01", "0.01"); |
| 75 |
bench_decimal_op!(mul_negative_point_five, *, "2.01", "-0.5"); |
| 76 |
bench_decimal_op!(mul_pi, *, "2.01", "3.1415926535897932384626433832"); |
| 77 |
bench_decimal_op!(mul_negative_pi, *, "2.01", "-3.1415926535897932384626433832"); |
| 78 |
|
| 79 |
bench_fold_op!(mul_25, *, Decimal::from_str("1.1").unwrap(), 25); |
| 80 |
|
| 81 |
/* Div */ |
| 82 |
bench_decimal_op!(div_one, /, "2.01", "1"); |
| 83 |
bench_decimal_op!(div_two, /, "2.01", "2"); |
| 84 |
bench_decimal_op!(div_one_hundred, /, "2.01", "100"); |
| 85 |
bench_decimal_op!(div_point_zero_one, /, "2.01", "0.01"); |
| 86 |
bench_decimal_op!(div_negative_point_five, /, "2.01", "-0.5"); |
| 87 |
bench_decimal_op!(div_pi, /, "2.01", "3.1415926535897932384626433832"); |
| 88 |
bench_decimal_op!(div_negative_pi, /, "2.01", "-3.1415926535897932384626433832"); |
| 89 |
bench_decimal_op!(div_no_underflow, /, "1.02343545345", "0.35454343453"); |
| 90 |
bench_fold_op!(div_10k, /, Decimal::MAX, 10_000); |
| 91 |
bench_fold_op!(rem_10k, %, Decimal::MAX, 10_000); |
| 92 |
|
| 93 |
/* Iteration */ |
| 94 |
struct DecimalIterator { |
| 95 |
count: usize, |
| 96 |
} |
| 97 |
|
| 98 |
impl DecimalIterator { |
| 99 |
fn new() -> DecimalIterator { |
| 100 |
DecimalIterator { count: 0 } |
| 101 |
} |
| 102 |
} |
| 103 |
|
| 104 |
impl Iterator for DecimalIterator { |
| 105 |
type Item = Decimal; |
| 106 |
|
| 107 |
fn next(&mut self) -> Option<Decimal> { |
| 108 |
self.count += 1; |
| 109 |
if self.count < 6 { |
| 110 |
Some(Decimal::new(314, 2)) |
| 111 |
} else { |
| 112 |
None |
| 113 |
} |
| 114 |
} |
| 115 |
} |
| 116 |
|
| 117 |
#[bench] |
| 118 |
fn iterator_individual(b: &mut ::test::Bencher) { |
| 119 |
b.iter(|| { |
| 120 |
let mut result = Decimal::new(0, 0); |
| 121 |
let iterator = DecimalIterator::new(); |
| 122 |
for i in iterator { |
| 123 |
result += i; |
| 124 |
} |
| 125 |
::test::black_box(result); |
| 126 |
}); |
| 127 |
} |
| 128 |
|
| 129 |
#[bench] |
| 130 |
fn iterator_product(b: &mut ::test::Bencher) { |
| 131 |
b.iter(|| { |
| 132 |
let result: Decimal = DecimalIterator::new().product(); |
| 133 |
::test::black_box(result); |
| 134 |
}); |
| 135 |
} |
| 136 |
|
| 137 |
#[bench] |
| 138 |
fn iterator_sum(b: &mut ::test::Bencher) { |
| 139 |
b.iter(|| { |
| 140 |
let result: Decimal = DecimalIterator::new().sum(); |
| 141 |
::test::black_box(result); |
| 142 |
}); |
| 143 |
} |
| 144 |
|
| 145 |
const SAMPLE_STRS: &[&str] = &[ |
| 146 |
"3950.123456", |
| 147 |
"3950", |
| 148 |
"0.1", |
| 149 |
"0.01", |
| 150 |
"0.001", |
| 151 |
"0.0001", |
| 152 |
"0.00001", |
| 153 |
"0.000001", |
| 154 |
"1", |
| 155 |
"-100", |
| 156 |
"-123.456", |
| 157 |
"119996.25", |
| 158 |
"1000000", |
| 159 |
"9999999.99999", |
| 160 |
"12340.56789", |
| 161 |
]; |
| 162 |
|
| 163 |
#[bench] |
| 164 |
fn serialize_bincode(b: &mut test::Bencher) { |
| 165 |
let decimals: Vec<Decimal> = SAMPLE_STRS.iter().map(|s| Decimal::from_str(s).unwrap()).collect(); |
| 166 |
|
| 167 |
b.iter(|| { |
| 168 |
for d in &decimals { |
| 169 |
let bytes = bincode::options().serialize(d).unwrap(); |
| 170 |
test::black_box(bytes); |
| 171 |
} |
| 172 |
}) |
| 173 |
} |
| 174 |
|
| 175 |
#[cfg(feature = "serde-str")] |
| 176 |
#[bench] |
| 177 |
fn deserialize_bincode(b: &mut test::Bencher) { |
| 178 |
let payloads: Vec<Vec<u8>> = SAMPLE_STRS |
| 179 |
.iter() |
| 180 |
.map(|s| bincode::options().serialize(&Decimal::from_str(s).unwrap()).unwrap()) |
| 181 |
.collect(); |
| 182 |
|
| 183 |
b.iter(|| { |
| 184 |
for payload in &payloads { |
| 185 |
let decimal: Decimal = bincode::options().deserialize(payload).unwrap(); |
| 186 |
test::black_box(decimal); |
| 187 |
} |
| 188 |
}) |
| 189 |
} |
| 190 |
|
| 191 |
#[bench] |
| 192 |
fn decimal_from_str(b: &mut test::Bencher) { |
| 193 |
b.iter(|| { |
| 194 |
for s in SAMPLE_STRS { |
| 195 |
let result = Decimal::from_str(s).unwrap(); |
| 196 |
test::black_box(result); |
| 197 |
} |
| 198 |
}) |
| 199 |
} |
| 200 |
|
| 201 |
#[bench] |
| 202 |
fn decimal_to_string(b: &mut test::Bencher) { |
| 203 |
let decimals: Vec<Decimal> = SAMPLE_STRS.iter().map(|s| Decimal::from_str(s).unwrap()).collect(); |
| 204 |
|
| 205 |
b.iter(|| { |
| 206 |
for s in decimals.iter() { |
| 207 |
let string = s.to_string(); |
| 208 |
test::black_box(string); |
| 209 |
} |
| 210 |
}) |
| 211 |
} |
| 212 |
|
| 213 |
#[cfg(feature = "postgres")] |
| 214 |
#[bench] |
| 215 |
fn to_from_sql(b: &mut ::test::Bencher) { |
| 216 |
use bytes::BytesMut; |
| 217 |
use postgres::types::{FromSql, Kind, ToSql, Type}; |
| 218 |
|
| 219 |
let samples: Vec<Decimal> = test::black_box(SAMPLE_STRS.iter().map(|x| Decimal::from_str(x).unwrap()).collect()); |
| 220 |
let t = Type::new("".into(), 0, Kind::Simple, "".into()); |
| 221 |
let mut bytes: BytesMut = BytesMut::with_capacity(100).into(); |
| 222 |
|
| 223 |
b.iter(|| { |
| 224 |
for _ in 0..100 { |
| 225 |
for sample in &samples { |
| 226 |
bytes.clear(); |
| 227 |
sample.to_sql(&t, &mut bytes).unwrap(); |
| 228 |
let result = Decimal::from_sql(&t, &bytes).unwrap(); |
| 229 |
::test::black_box(result); |
| 230 |
} |
| 231 |
} |
| 232 |
}); |
| 233 |
} |
| 234 |
|
| 235 |
#[cfg(feature = "maths")] |
| 236 |
mod maths { |
| 237 |
use rust_decimal::prelude::*; |
| 238 |
|
| 239 |
#[bench] |
| 240 |
fn powi(b: &mut ::test::Bencher) { |
| 241 |
// These exponents have to be fairly small because multiplcation overflows easily |
| 242 |
let samples = &[ |
| 243 |
(Decimal::from_str("36.7").unwrap(), 5), |
| 244 |
(Decimal::from_str("0.00000007").unwrap(), 5), |
| 245 |
(Decimal::from(2), 64), |
| 246 |
(Decimal::from_str("8819287.19276555").unwrap(), 3), |
| 247 |
(Decimal::from_str("-8819287.19276555").unwrap(), 3), |
| 248 |
]; |
| 249 |
b.iter(|| { |
| 250 |
for sample in samples.iter() { |
| 251 |
let result = sample.0.powi(sample.1); |
| 252 |
::test::black_box(result); |
| 253 |
} |
| 254 |
}); |
| 255 |
} |
| 256 |
|
| 257 |
#[bench] |
| 258 |
fn sqrt(b: &mut ::test::Bencher) { |
| 259 |
let samples = &[ |
| 260 |
Decimal::from_str("36.7").unwrap(), |
| 261 |
Decimal::from_str("0.00000007").unwrap(), |
| 262 |
Decimal::from(2), |
| 263 |
Decimal::from_str("8819287.19276555").unwrap(), |
| 264 |
Decimal::from_str("-8819287.19276555").unwrap(), |
| 265 |
]; |
| 266 |
b.iter(|| { |
| 267 |
for sample in samples.iter() { |
| 268 |
let result = sample.sqrt(); |
| 269 |
::test::black_box(result); |
| 270 |
} |
| 271 |
}); |
| 272 |
} |
| 273 |
|
| 274 |
#[bench] |
| 275 |
fn exp(b: &mut ::test::Bencher) { |
| 276 |
let samples = &[ |
| 277 |
Decimal::from_str("3.7").unwrap(), |
| 278 |
Decimal::from_str("0.07").unwrap(), |
| 279 |
Decimal::from(2), |
| 280 |
Decimal::from_str("8.19").unwrap(), |
| 281 |
Decimal::from_str("-8.19").unwrap(), |
| 282 |
]; |
| 283 |
b.iter(|| { |
| 284 |
for sample in samples.iter() { |
| 285 |
let result = sample.exp(); |
| 286 |
::test::black_box(result); |
| 287 |
} |
| 288 |
}); |
| 289 |
} |
| 290 |
|
| 291 |
#[bench] |
| 292 |
fn norm_cdf(b: &mut ::test::Bencher) { |
| 293 |
let samples = &[ |
| 294 |
Decimal::from_str("3.7").unwrap(), |
| 295 |
Decimal::from_str("0.007").unwrap(), |
| 296 |
Decimal::from(2), |
| 297 |
Decimal::from_str("1.19").unwrap(), |
| 298 |
Decimal::from_str("-1.19").unwrap(), |
| 299 |
]; |
| 300 |
b.iter(|| { |
| 301 |
for sample in samples.iter() { |
| 302 |
let result = sample.norm_cdf(); |
| 303 |
::test::black_box(result); |
| 304 |
} |
| 305 |
}); |
| 306 |
} |
| 307 |
|
| 308 |
#[bench] |
| 309 |
fn norm_pdf(b: &mut ::test::Bencher) { |
| 310 |
let samples = &[ |
| 311 |
Decimal::from_str("3.7").unwrap(), |
| 312 |
Decimal::from_str("0.007").unwrap(), |
| 313 |
Decimal::from(2), |
| 314 |
Decimal::from_str("1.19").unwrap(), |
| 315 |
Decimal::from_str("-1.19").unwrap(), |
| 316 |
]; |
| 317 |
b.iter(|| { |
| 318 |
for sample in samples.iter() { |
| 319 |
let result = sample.norm_pdf(); |
| 320 |
::test::black_box(result); |
| 321 |
} |
| 322 |
}); |
| 323 |
} |
| 324 |
|
| 325 |
#[bench] |
| 326 |
fn ln(b: &mut ::test::Bencher) { |
| 327 |
let samples = &[ |
| 328 |
Decimal::from_str("36.7").unwrap(), |
| 329 |
Decimal::from_str("0.00000007").unwrap(), |
| 330 |
Decimal::from(2), |
| 331 |
Decimal::from_str("8819287.19").unwrap(), |
| 332 |
Decimal::from_str("-8819287.19").unwrap(), |
| 333 |
]; |
| 334 |
b.iter(|| { |
| 335 |
for sample in samples.iter() { |
| 336 |
let result = sample.ln(); |
| 337 |
::test::black_box(result); |
| 338 |
} |
| 339 |
}); |
| 340 |
} |
| 341 |
|
| 342 |
#[bench] |
| 343 |
fn erf(b: &mut ::test::Bencher) { |
| 344 |
let samples = &[ |
| 345 |
Decimal::from(0), |
| 346 |
Decimal::from(1), |
| 347 |
Decimal::from_str("-0.98717").unwrap(), |
| 348 |
Decimal::from_str("0.07").unwrap(), |
| 349 |
Decimal::from_str("0.1111").unwrap(), |
| 350 |
Decimal::from_str("0.4").unwrap(), |
| 351 |
]; |
| 352 |
b.iter(|| { |
| 353 |
for sample in samples.iter() { |
| 354 |
let result = sample.erf(); |
| 355 |
::test::black_box(result); |
| 356 |
} |
| 357 |
}); |
| 358 |
} |
| 359 |
} |