| 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 |
|
| 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 |
|
| 56 |
bench_decimal_op!(add_64bit_aligned, +, "12345678901.23", "98765432109.87"); |
| 57 |
|
| 58 |
bench_decimal_op!(add_large_scale_diff, +, "12345678", "0.0000000000000000000000000001"); |
| 59 |
|
| 60 |
bench_decimal_op!(add_96bit_aligned, +, "79228162514264337593543950330", "5"); |
| 61 |
|
| 62 |
bench_decimal_op!(add_96bit_sub, +, "79228162514264337593543950335", "-79228162514264337593543950334"); |
| 63 |
|
| 64 |
bench_fold_op!(add_10k, +, 0, 10_000); |
| 65 |
|
| 66 |
|
| 67 |
bench_decimal_op!(sub_self, -, "2.01", "2.01"); |
| 68 |
bench_decimal_op!(sub_simple, -, "2", "1"); |
| 69 |
bench_decimal_op!(sub_one, -, "2.01", "1"); |
| 70 |
bench_decimal_op!(sub_two, -, "2.01", "2"); |
| 71 |
bench_decimal_op!(sub_one_hundred, -, "2.01", "100"); |
| 72 |
bench_decimal_op!(sub_point_zero_one, -, "2.01", "0.01"); |
| 73 |
bench_decimal_op!(sub_negative_point_five, -, "2.01", "-0.5"); |
| 74 |
bench_decimal_op!(sub_pi, -, "2.01", "3.1415926535897932384626433832"); |
| 75 |
bench_decimal_op!(sub_negative_pi, -, "2.01", "-3.1415926535897932384626433832"); |
| 76 |
|
| 77 |
|
| 78 |
bench_decimal_op!(sub_64bit_aligned, -, "98765432109.87", "12345678901.23"); |
| 79 |
|
| 80 |
bench_decimal_op!(sub_large_scale_diff, -, "12345678", "0.0000000000000000000000000001"); |
| 81 |
|
| 82 |
bench_fold_op!(sub_10k, -, 5_000_000, 10_000); |
| 83 |
|
| 84 |
|
| 85 |
bench_decimal_op!(mul_one, *, "2.01", "1"); |
| 86 |
bench_decimal_op!(mul_two, *, "2.01", "2"); |
| 87 |
bench_decimal_op!(mul_one_hundred, *, "2.01", "100"); |
| 88 |
bench_decimal_op!(mul_point_zero_one, *, "2.01", "0.01"); |
| 89 |
bench_decimal_op!(mul_negative_point_five, *, "2.01", "-0.5"); |
| 90 |
bench_decimal_op!(mul_pi, *, "2.01", "3.1415926535897932384626433832"); |
| 91 |
bench_decimal_op!(mul_negative_pi, *, "2.01", "-3.1415926535897932384626433832"); |
| 92 |
|
| 93 |
|
| 94 |
bench_decimal_op!(mul_64bit_by_32bit, *, "12345678901.23", "7"); |
| 95 |
|
| 96 |
bench_decimal_op!(mul_64bit_by_64bit, *, "12345678901.23", "98765432109.87"); |
| 97 |
|
| 98 |
bench_decimal_op!(mul_96bit_by_32bit, *, "7922816251426433759354.3950335", "2"); |
| 99 |
|
| 100 |
bench_decimal_op!(mul_overflow_rescale, *, "99999999999999.9999999999", "99999999999999.9999999999"); |
| 101 |
|
| 102 |
bench_decimal_op!(mul_high_scale, *, "0.0000000000000001", "0.0000000000000001"); |
| 103 |
|
| 104 |
bench_fold_op!(mul_25, *, Decimal::from_str("1.1").unwrap(), 25); |
| 105 |
|
| 106 |
|
| 107 |
bench_decimal_op!(div_one, /, "2.01", "1"); |
| 108 |
bench_decimal_op!(div_two, /, "2.01", "2"); |
| 109 |
bench_decimal_op!(div_one_hundred, /, "2.01", "100"); |
| 110 |
bench_decimal_op!(div_point_zero_one, /, "2.01", "0.01"); |
| 111 |
bench_decimal_op!(div_negative_point_five, /, "2.01", "-0.5"); |
| 112 |
bench_decimal_op!(div_pi, /, "2.01", "3.1415926535897932384626433832"); |
| 113 |
bench_decimal_op!(div_negative_pi, /, "2.01", "-3.1415926535897932384626433832"); |
| 114 |
bench_decimal_op!(div_no_underflow, /, "1.02343545345", "0.35454343453"); |
| 115 |
bench_decimal_op!(div_32bit_divisor, /, "999999999999.999999", "7"); |
| 116 |
bench_decimal_op!(div_64bit_divisor, /, "123456789.123456789", "123456.789"); |
| 117 |
bench_decimal_op!(div_96bit_divisor, /, "79228162514264337593543950335", "79228162514264337593.543950335"); |
| 118 |
bench_decimal_op!(div_repeating, /, "10", "3"); |
| 119 |
bench_decimal_op!(div_self, /, "3.1415926535897932384626433832", "3.1415926535897932384626433832"); |
| 120 |
bench_fold_op!(div_10k, /, Decimal::MAX, 10_000); |
| 121 |
bench_fold_op!(rem_10k, %, Decimal::MAX, 10_000); |
| 122 |
|
| 123 |
|
| 124 |
struct DecimalIterator { |
| 125 |
count: usize, |
| 126 |
} |
| 127 |
|
| 128 |
impl DecimalIterator { |
| 129 |
fn new() -> DecimalIterator { |
| 130 |
DecimalIterator { count: 0 } |
| 131 |
} |
| 132 |
} |
| 133 |
|
| 134 |
impl Iterator for DecimalIterator { |
| 135 |
type Item = Decimal; |
| 136 |
|
| 137 |
fn next(&mut self) -> Option<Decimal> { |
| 138 |
self.count += 1; |
| 139 |
if self.count < 6 { |
| 140 |
Some(Decimal::new(314, 2)) |
| 141 |
} else { |
| 142 |
None |
| 143 |
} |
| 144 |
} |
| 145 |
} |
| 146 |
|
| 147 |
#[bench] |
| 148 |
fn iterator_individual(b: &mut ::test::Bencher) { |
| 149 |
b.iter(|| { |
| 150 |
let mut result = Decimal::new(0, 0); |
| 151 |
let iterator = DecimalIterator::new(); |
| 152 |
for i in iterator { |
| 153 |
result += i; |
| 154 |
} |
| 155 |
::test::black_box(result); |
| 156 |
}); |
| 157 |
} |
| 158 |
|
| 159 |
#[bench] |
| 160 |
fn iterator_product(b: &mut ::test::Bencher) { |
| 161 |
b.iter(|| { |
| 162 |
let result: Decimal = DecimalIterator::new().product(); |
| 163 |
::test::black_box(result); |
| 164 |
}); |
| 165 |
} |
| 166 |
|
| 167 |
#[bench] |
| 168 |
fn iterator_sum(b: &mut ::test::Bencher) { |
| 169 |
b.iter(|| { |
| 170 |
let result: Decimal = DecimalIterator::new().sum(); |
| 171 |
::test::black_box(result); |
| 172 |
}); |
| 173 |
} |
| 174 |
|
| 175 |
const SAMPLE_STRS: &[&str] = &[ |
| 176 |
"3950.123456", |
| 177 |
"3950", |
| 178 |
"0.1", |
| 179 |
"0.01", |
| 180 |
"0.001", |
| 181 |
"0.0001", |
| 182 |
"0.00001", |
| 183 |
"0.000001", |
| 184 |
"1", |
| 185 |
"-100", |
| 186 |
"-123.456", |
| 187 |
"119996.25", |
| 188 |
"1000000", |
| 189 |
"9999999.99999", |
| 190 |
"12340.56789", |
| 191 |
]; |
| 192 |
|
| 193 |
#[bench] |
| 194 |
fn serialize_bincode(b: &mut test::Bencher) { |
| 195 |
let decimals: Vec<Decimal> = SAMPLE_STRS.iter().map(|s| Decimal::from_str(s).unwrap()).collect(); |
| 196 |
|
| 197 |
b.iter(|| { |
| 198 |
for d in &decimals { |
| 199 |
let bytes = bincode::options().serialize(d).unwrap(); |
| 200 |
test::black_box(bytes); |
| 201 |
} |
| 202 |
}) |
| 203 |
} |
| 204 |
|
| 205 |
#[cfg(feature = "serde-str")] |
| 206 |
#[bench] |
| 207 |
fn deserialize_bincode(b: &mut test::Bencher) { |
| 208 |
let payloads: Vec<Vec<u8>> = SAMPLE_STRS |
| 209 |
.iter() |
| 210 |
.map(|s| bincode::options().serialize(&Decimal::from_str(s).unwrap()).unwrap()) |
| 211 |
.collect(); |
| 212 |
|
| 213 |
b.iter(|| { |
| 214 |
for payload in &payloads { |
| 215 |
let decimal: Decimal = bincode::options().deserialize(payload).unwrap(); |
| 216 |
test::black_box(decimal); |
| 217 |
} |
| 218 |
}) |
| 219 |
} |
| 220 |
|
| 221 |
#[bench] |
| 222 |
fn decimal_from_str(b: &mut test::Bencher) { |
| 223 |
b.iter(|| { |
| 224 |
for s in SAMPLE_STRS { |
| 225 |
let result = Decimal::from_str(s).unwrap(); |
| 226 |
test::black_box(result); |
| 227 |
} |
| 228 |
}) |
| 229 |
} |
| 230 |
|
| 231 |
#[bench] |
| 232 |
fn decimal_to_string(b: &mut test::Bencher) { |
| 233 |
let decimals: Vec<Decimal> = SAMPLE_STRS.iter().map(|s| Decimal::from_str(s).unwrap()).collect(); |
| 234 |
|
| 235 |
b.iter(|| { |
| 236 |
for s in decimals.iter() { |
| 237 |
let string = s.to_string(); |
| 238 |
test::black_box(string); |
| 239 |
} |
| 240 |
}) |
| 241 |
} |
| 242 |
|
| 243 |
#[cfg(feature = "postgres")] |
| 244 |
#[bench] |
| 245 |
fn to_from_sql(b: &mut ::test::Bencher) { |
| 246 |
use bytes::BytesMut; |
| 247 |
use postgres::types::{FromSql, Kind, ToSql, Type}; |
| 248 |
|
| 249 |
let samples: Vec<Decimal> = test::black_box(SAMPLE_STRS.iter().map(|x| Decimal::from_str(x).unwrap()).collect()); |
| 250 |
let t = Type::new("".into(), 0, Kind::Simple, "".into()); |
| 251 |
let mut bytes: BytesMut = BytesMut::with_capacity(100).into(); |
| 252 |
|
| 253 |
b.iter(|| { |
| 254 |
for _ in 0..100 { |
| 255 |
for sample in &samples { |
| 256 |
bytes.clear(); |
| 257 |
sample.to_sql(&t, &mut bytes).unwrap(); |
| 258 |
let result = Decimal::from_sql(&t, &bytes).unwrap(); |
| 259 |
::test::black_box(result); |
| 260 |
} |
| 261 |
} |
| 262 |
}); |
| 263 |
} |
| 264 |
|
| 265 |
#[cfg(feature = "maths")] |
| 266 |
mod maths { |
| 267 |
use rust_decimal::prelude::*; |
| 268 |
|
| 269 |
#[bench] |
| 270 |
fn powi(b: &mut ::test::Bencher) { |
| 271 |
|
| 272 |
let samples = &[ |
| 273 |
(Decimal::from_str("36.7").unwrap(), 5), |
| 274 |
(Decimal::from_str("0.00000007").unwrap(), 5), |
| 275 |
(Decimal::from(2), 64), |
| 276 |
(Decimal::from_str("8819287.19276555").unwrap(), 3), |
| 277 |
(Decimal::from_str("-8819287.19276555").unwrap(), 3), |
| 278 |
]; |
| 279 |
b.iter(|| { |
| 280 |
for sample in samples.iter() { |
| 281 |
let result = sample.0.powi(sample.1); |
| 282 |
::test::black_box(result); |
| 283 |
} |
| 284 |
}); |
| 285 |
} |
| 286 |
|
| 287 |
#[bench] |
| 288 |
fn sqrt(b: &mut ::test::Bencher) { |
| 289 |
let samples = &[ |
| 290 |
Decimal::from_str("36.7").unwrap(), |
| 291 |
Decimal::from_str("0.00000007").unwrap(), |
| 292 |
Decimal::from(2), |
| 293 |
Decimal::from_str("8819287.19276555").unwrap(), |
| 294 |
Decimal::from_str("-8819287.19276555").unwrap(), |
| 295 |
]; |
| 296 |
b.iter(|| { |
| 297 |
for sample in samples.iter() { |
| 298 |
let result = sample.sqrt(); |
| 299 |
::test::black_box(result); |
| 300 |
} |
| 301 |
}); |
| 302 |
} |
| 303 |
|
| 304 |
#[bench] |
| 305 |
fn exp(b: &mut ::test::Bencher) { |
| 306 |
let samples = &[ |
| 307 |
Decimal::from_str("3.7").unwrap(), |
| 308 |
Decimal::from_str("0.07").unwrap(), |
| 309 |
Decimal::from(2), |
| 310 |
Decimal::from_str("8.19").unwrap(), |
| 311 |
Decimal::from_str("-8.19").unwrap(), |
| 312 |
]; |
| 313 |
b.iter(|| { |
| 314 |
for sample in samples.iter() { |
| 315 |
let result = sample.exp(); |
| 316 |
::test::black_box(result); |
| 317 |
} |
| 318 |
}); |
| 319 |
} |
| 320 |
|
| 321 |
#[bench] |
| 322 |
fn exp_large(b: &mut ::test::Bencher) { |
| 323 |
let samples = &[ |
| 324 |
Decimal::from_str("17.6").unwrap(), |
| 325 |
Decimal::from_str("34.5").unwrap(), |
| 326 |
Decimal::from_str("54.3").unwrap(), |
| 327 |
]; |
| 328 |
b.iter(|| { |
| 329 |
for sample in samples.iter() { |
| 330 |
let result = sample.exp(); |
| 331 |
::test::black_box(result); |
| 332 |
} |
| 333 |
}); |
| 334 |
} |
| 335 |
|
| 336 |
#[bench] |
| 337 |
fn norm_cdf(b: &mut ::test::Bencher) { |
| 338 |
let samples = &[ |
| 339 |
Decimal::from_str("3.7").unwrap(), |
| 340 |
Decimal::from_str("0.007").unwrap(), |
| 341 |
Decimal::from(2), |
| 342 |
Decimal::from_str("1.19").unwrap(), |
| 343 |
Decimal::from_str("-1.19").unwrap(), |
| 344 |
]; |
| 345 |
b.iter(|| { |
| 346 |
for sample in samples.iter() { |
| 347 |
let result = sample.norm_cdf(); |
| 348 |
::test::black_box(result); |
| 349 |
} |
| 350 |
}); |
| 351 |
} |
| 352 |
|
| 353 |
#[bench] |
| 354 |
fn norm_pdf(b: &mut ::test::Bencher) { |
| 355 |
let samples = &[ |
| 356 |
Decimal::from_str("3.7").unwrap(), |
| 357 |
Decimal::from_str("0.007").unwrap(), |
| 358 |
Decimal::from(2), |
| 359 |
Decimal::from_str("1.19").unwrap(), |
| 360 |
Decimal::from_str("-1.19").unwrap(), |
| 361 |
]; |
| 362 |
b.iter(|| { |
| 363 |
for sample in samples.iter() { |
| 364 |
let result = sample.norm_pdf(); |
| 365 |
::test::black_box(result); |
| 366 |
} |
| 367 |
}); |
| 368 |
} |
| 369 |
|
| 370 |
#[bench] |
| 371 |
fn ln(b: &mut ::test::Bencher) { |
| 372 |
let samples = &[ |
| 373 |
Decimal::from_str("36.7").unwrap(), |
| 374 |
Decimal::from_str("0.00000007").unwrap(), |
| 375 |
Decimal::from(2), |
| 376 |
Decimal::from_str("8819287.19").unwrap(), |
| 377 |
]; |
| 378 |
b.iter(|| { |
| 379 |
for sample in samples.iter() { |
| 380 |
let result = sample.checked_ln(); |
| 381 |
::test::black_box(result); |
| 382 |
} |
| 383 |
}); |
| 384 |
} |
| 385 |
|
| 386 |
#[bench] |
| 387 |
fn erf(b: &mut ::test::Bencher) { |
| 388 |
let samples = &[ |
| 389 |
Decimal::from(0), |
| 390 |
Decimal::from(1), |
| 391 |
Decimal::from_str("-0.98717").unwrap(), |
| 392 |
Decimal::from_str("0.07").unwrap(), |
| 393 |
Decimal::from_str("0.1111").unwrap(), |
| 394 |
Decimal::from_str("0.4").unwrap(), |
| 395 |
]; |
| 396 |
b.iter(|| { |
| 397 |
for sample in samples.iter() { |
| 398 |
let result = sample.erf(); |
| 399 |
::test::black_box(result); |
| 400 |
} |
| 401 |
}); |
| 402 |
} |
| 403 |
} |