0

我编写了相同的函数来计算 udf 函数,


trait myfun {
    fn get_res(self) -> Self;
    fn get_res2(self) -> Self;
}

impl myfun for Expr {
    fn get_res(self) -> Self {
        let mut a: Expr = lit(0.0);
        let max_day = 5;

        for i in 0..max_day {
            a = a
                + self.clone().slice(i * 48 + 36, 12).mean()
                    / self.clone().slice(i * 48 + 24, 24).mean()
                    / lit(max_day - i)
                + self.clone().slice(i * 48 + 42, 6).mean()
                    / self.clone().slice(i * 48 + 36, 12).mean()
                    / lit(max_day - i)
                - lit(2.0);
        }

        a
    }
    fn get_res2(self) -> Expr {
        let u = self.apply(
            |s| {
                let max_day = 5;
                let mut a = 0.0;
                if s.len() as i64 == max_day * 48 {
                    for i in 0..max_day {
                        a += s.slice(i * 48 + 36, 12).mean().unwrap()
                            / s.slice(i * 48 + 24, 24).mean().unwrap()
                            / (max_day - i) as f64
                            + s.slice(i * 48 + 42, 6).mean().unwrap()
                                / s.slice(i * 48 + 36, 12).mean().unwrap()
                                / (max_day - i) as f64
                            - 2.0;
                    }
                }

                Ok(Series::new("name", vec![a]))
            },
            Default::default(),
        );

        u
    }
}

第一个在极地使用纯 ops Expr

[([([([([([([([([([([([([([([(Float64(0.0)) Plus ([([(AGG MEAN SLICE close offset: 36 len: 12) Divide (AGG MEAN SLICE close offset: 24 len: 24)]) Divide (Int64(5))])]) Plus ([([(AGG MEAN SLICE close offset: 42 len: 6) Divide (AGG MEAN SLICE close offset: 36 len: 12)]) Divide (Int64(5))])]) Minus (Float64(2.0))]) Plus ([([(AGG MEAN SLICE close offset: 84 len: 12) Divide (AGG MEAN SLICE close offset: 72 len: 24)]) Divide (Int64(4))])]) Plus ([([(AGG MEAN SLICE close offset: 90 len: 6) Divide (AGG MEAN SLICE close offset: 84 len: 12)]) Divide (Int64(4))])]) Minus (Float64(2.0))]) Plus ([([(AGG MEAN SLICE close offset: 132 len: 12) Divide (AGG MEAN SLICE close offset: 120 len: 24)]) Divide (Int64(3))])]) Plus ([([(AGG MEAN SLICE close offset: 138 len: 6) Divide (AGG MEAN SLICE close offset: 132 len: 12)]) Divide (Int64(3))])]) Minus (Float64(2.0))]) Plus ([([(AGG MEAN SLICE close offset: 180 len: 12) Divide (AGG MEAN SLICE close offset: 168 len: 24)]) Divide (Int64(2))])]) Plus ([([(AGG MEAN SLICE close offset: 186 len: 6) Divide (AGG MEAN SLICE close offset: 180 len: 12)]) Divide (Int64(2))])]) Minus (Float64(2.0))]) Plus ([([(AGG MEAN SLICE close offset: 228 len: 12) Divide (AGG MEAN SLICE close offset: 216 len: 24)]) Divide (Int64(1))])]) Plus ([([(AGG MEAN SLICE close offset: 234 len: 6) Divide (AGG MEAN SLICE close offset: 228 len: 12)]) Divide (Int64(1))])]) Minus (Float64(2.0))]

第二个是

APPLY([close])

纯 expr 版本耗时约 250ms,apply func 耗时约 12ms

我的操作有什么问题吗?

4

0 回答 0