#[cfg(test)] mod tests { use std::cmp::{max, min}; use std::str::FromStr; use std::time::{Instant, Duration}; use rust_decimal::Decimal; use rust_decimal::prelude::{FromPrimitive, ToPrimitive}; use rust_decimal_macros::dec; #[test] // 计算十秒内能执行多少次decimal计算 fn test_decimal_subtraction_performance() { // decimal使用的参考文档:https://docs.rs/rust_decimal/latest/rust_decimal/ // 在AMD Ryzen 7 5800X 8-Core Processor下,Decimal平均每秒可以计算1200w次(28位小数)~1600w次(1位小数) let mut count = 0u64; let point_3 = dec!(0.3323289834012382184823981293); let point_2 = dec!(0.2518364912329136932183926421); let num = point_3 - point_2; let seconds = 10; let start = Instant::now(); while start.elapsed() < Duration::from_secs(seconds) { let _ = point_3 - point_2; count += 1; } println!("Performed {} decimal subtractions in one second(avg), result is {:?}.", count / seconds, num.to_f64().unwrap()); } #[test] fn test_decimal_equals() { let a = dec!(0.123234213); let b = dec!(0.123234213); println!("{} == {} is {}", a, b, a == b); println!("{}.eq({}) is {}", a, b, a.eq(&b)); } #[test] fn test_decimal_min_and_max() { let a = dec!(0.123234213); let b = dec!(0.823004213); println!("max({}, {})={}", a, b, max(a, b)); println!("min({}, {})={}", a, b, min(a, b)); } #[test] fn test_decimal_from() { let a = dec!(1e-6); let b = Decimal::from_f64(f64::from_str("1e-6").unwrap()).unwrap(); println!("a={}", a); println!("b={:?}", b); } }