Sfoglia il codice sorgente

涉及到计算部分的f64全部改成Decimal,经测试,效率比较接近(大概损失40%的性能),可以接受。

skyfffire 2 anni fa
parent
commit
c3b2e071ff
4 ha cambiato i file con 60 aggiunte e 0 eliminazioni
  1. 2 0
      strategy/Cargo.toml
  2. 1 0
      strategy/src/lib.rs
  3. 28 0
      strategy/tests/decimal_test.rs
  4. 29 0
      strategy/tests/f64_test.rs

+ 2 - 0
strategy/Cargo.toml

@@ -11,3 +11,5 @@ serde_derive = "1.0"
 serde_json = "1.0.104"
 toml = "0.5"
 tokio = { version = "1.31.0", features = ["full"] }
+rust_decimal = "1.32.0"
+rust_decimal_macros = "1.32.0"

+ 1 - 0
strategy/src/lib.rs

@@ -0,0 +1 @@
+pub mod predictor;

+ 28 - 0
strategy/tests/decimal_test.rs

@@ -0,0 +1,28 @@
+use tokio;
+
+#[cfg(test)]
+mod tests {
+    use std::time::{Instant, Duration};
+    use rust_decimal::prelude::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());
+    }
+}

+ 29 - 0
strategy/tests/f64_test.rs

@@ -0,0 +1,29 @@
+use tokio;
+
+#[tokio::test]
+async fn f64_tests() {
+    println!("{:?}", 0.3f64 - 0.1f64);
+    println!("{:?}", 0.3f64 - 0.2f64);
+}
+
+#[cfg(test)]
+mod tests {
+    use std::time::{Instant, Duration};
+
+    #[test]
+    // 计算十秒内能执行多少次f64计算
+    fn test_f64_subtraction_performance() {
+        // 在AMD Ryzen 7 5800X 8-Core Processor下,f64平均每秒可以计算2300w次~2600w次(1位小数)
+        let mut count = 0u64;
+        let num = 0.3f64 - 0.2f64;
+        let seconds = 10;
+
+        let start = Instant::now();
+        while start.elapsed() < Duration::from_secs(seconds) {
+            let _ = 0.3f64 - 0.2f64;
+            count += 1;
+        }
+
+        println!("Performed {} f64 subtractions in one second(avg), result is {}.", count / seconds, num);
+    }
+}