Why is standard R median function so much slower than a simple C++ alternative?

前端 未结 3 1755
鱼传尺愫
鱼传尺愫 2021-02-06 15:54

I made the following implementation of the median in C++ and and used it in R via Rcpp:

// [[Rcpp::export]]
double median2         


        
3条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-02-06 16:37

    I'm not sure what "standard" implementation you would be referring to.

    Anyway: If there were one, it would, being part of a standard library, certainly not be allowed to change the order of elements in the vector (as your implementation does), so it would definitely have to work on a copy.

    Creating this copy would take time and CPU (and significant memory), which would affect the run time.

提交回复
热议问题