Why is mean() so slow?

后端 未结 2 525
情话喂你
情话喂你 2020-12-08 14:37

Everything is in the question! I just tried to do a bit of optimization, and nailing down the bottle necks, out of curiosity, I tried that:

t1 <- rnorm(10         


        
2条回答
  •  半阙折子戏
    2020-12-08 14:58

    mean is slower than computing "by hand" for several reasons:

    1. S3 Method dispatch
    2. NA handling
    3. Error correction

    Points 1 and 2 have already been covered. Point 3 is discussed in What algorithm is R using to calculate mean?. Basically, mean makes 2 passes over the vector in order to correct for floating point errors. sum only makes 1 pass over the vector.

    Notice that identical(sum(t1)/length(t1), mean(t1)) may be FALSE, due to these precision issues.

    > set.seed(21); t1 <- rnorm(1e7,,21)
    > identical(sum(t1)/length(t1), mean(t1))
    [1] FALSE
    > sum(t1)/length(t1) - mean(t1)
    [1] 2.539201e-16
    

提交回复
热议问题