Rcpp 可以替换 R 中的 unif 函数吗? [英] Can Rcpp replace unif function in R?

查看:57
本文介绍了Rcpp 可以替换 R 中的 unif 函数吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚开始在 R 中使用 Rcpp 包,我的学习灵感来自 Hadley Wickham 的高级 R 课程.

I have just started using the Rcpp package in R, my learning is inspired by the Advanced R course by Hadley Wickham.

在 R studio 中,我有以下 .cpp 文件.这个问题更笼统,但这个例子有帮助.

Within R studio I have the following .cpp file. The question is more general but this example helps.

#include <Rcpp.h>
using namespace Rcpp;

// [[Rcpp::export]]
NumericVector runifC(int n, double min=0, double max=1) {
  NumericVector out(n);

  for(int i = 0; i < n; ++i) {
    out[i] = min + ((double) rand() / (RAND_MAX)) * (max - min);
  }
  return out;
}

/*** R
library(microbenchmark)
microbenchmark(
  'R unif-1'      = runif(1),
  'C++ unif-1'    = runifC(1),
  'R unif-100'    = runif(100),
  'C++ unif-100'  = runifC(100),
  'R unif-1000'   = runif(1000),
  'C++ unif-1000' = runifC(1000),
  'R unif-100000'   = runif(100000),
  'C++ unif-100000' = runifC(100000)
)
*/

当我获取/保存文件时,它会显示性能输出.

When I source/save the file it shows me the performance output.

Unit: nanoseconds
             expr     min        lq       mean    median        uq     max neval
         R unif-1    2061    2644.5    4000.71    3456.0    4297.0   15402   100
       C++ unif-1     710    1190.0    1815.11    1685.0    2168.5    5776   100
       R unif-100    4717    5566.5    6794.14    6563.0    7435.5   16600   100
     C++ unif-100    1450    1997.5    2663.29    2591.5    3107.0    5307   100
      R unif-1000   28210   29584.5   31310.54   30380.0   31599.0   52879   100
    C++ unif-1000    8292    8951.0   10113.78    9462.5   10121.5   25099   100
    R unif-100000 2642581 2975117.0 3104580.62 3030938.5 3119489.0 5435046   100
  C++ unif-100000  699833  990924.0 1058855.49 1034430.5 1075078.0 1530351   100

我希望 runif 是一个非常优化的函数,但 C++ 代码的运行效率要高得多.我在这里可能很幼稚,但是如果性能存在如此大的差异,那么为什么不是所有适用的 R 函数都用 C++ 重写?

I would expect that runif would be a very optimised function but the C++ code runs much more efficiently. I might be naive here, but if there is such a difference in performance then why aren't all applicable R functions rewritten in C++?

很明显,有很多可能的改进,我觉得好像我错过了一个重要的原因,为什么不是所有的 R 函数都可以盲目地复制到 C++ 以提高性能.

It seems so obvious that there are a lot of improvements possible that I feel as if I am missing a huge reason of why not all R functions can be blindly copied to C++ for performance.

编辑:对于这个例子,rand() 的 C++ 实现有轻微的缺陷.我注意到的性能差距最常使用 rand() 函数.其他功能的性能似乎没有那么激烈,所以我更改了问题的名称.

edit: for this example it has been shown that the C++ implementation of rand() is slightly flawed. the performance gap that I noticed most used the rand() function. performance of other functions doesn't seem as drastic so i changed the name of the question.

推荐答案

请不要使用 rand().如果您提交它,这样做也会将您的包裹从 CRAN 中删除.

Please DO NOT USE rand(). Doing so will kick your package off CRAN too should you submit it.

参见例如这个 C++ 参考页的警告:

注意事项

无法保证生成的随机序列的质量.过去,rand() 的一些实现在产生序列的随机性、分布和周期方面存在严重缺陷(在一个众所周知的例子中,低位在调用之间简单地在 1 和 0 之间交替).

There are no guarantees as to the quality of the random sequence produced. In the past, some implementations of rand() have had serious shortcomings in the randomness, distribution and period of the sequence produced (in one well-known example, the low-order bit simply alternated between 1 and 0 between calls).

如果您对替代随机数生成器和计时感兴趣,请访问 Rcpp Gallery.

If you are interested in alternate random number generators and timing, the Rcpp Gallery.

一般来说,使用 R 提供的生成器,这些生成器具有出色的统计质量,并由 Rcpp 以标量和矢量化形式(Rcpp Sugar")提供.

In general, use the generators provided by R which are of excellent statistical quality, and offered in both scalar and vectorised form ("Rcpp Sugar") by Rcpp.

这篇关于Rcpp 可以替换 R 中的 unif 函数吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆