c++ - 在 R 中快速重复构建包

标签 c++ r rcpp

我有一个用 R 编写的包,其中包含大量复杂的 C++ 代码,需要一段时间才能编译。

当我更改代码时,我想快速重建包以便测试它。然而,R CMD build 似乎每次都是从头开始,而不是使用我的代码的 makefile 来只做需要的事情。

有没有办法在 R 中快速重复构建包以进行测试?

最佳答案

关于此的简短博文有点过期,但我之前提到过几次:使用 ccache .当文件没有改变时(即当你只是改变帮助页面时),或者当很少有文件改变时,它会有很大的帮助。缓存是一个非常聪明的技巧,并且包是健壮的。

在 Ubuntu/Debian 上:sudo apt-get install ccache 然后是例如这在你的 ~/.R/Makevars 中:

VER=
CCACHE=ccache
#CCACHE=
CC=$(CCACHE) gcc$(VER)
CXX=$(CCACHE) g++$(VER)
CXX11=$(CCACHE) g++$(VER)
CXX14=$(CCACHE) g++$(VER)

这也允许在 g++ 版本之间切换。将其更改为 clang++ 作为练习留给读者 ;-)

除此之外,查看 R CMD buildR CMD INSTALL 的选项以跳过 vignette 和/或手动构建以进一步加快重新构建。

插图:这是 Rcpp 本身的重新安装(从 git pull 重新安装),第一次安装在我工作的(体面的)机器上需要 21.9 秒,第二次只需要 1.4 秒感谢 ccache:

~/git/rcpp(master)$ time R CMD INSTALL .
* installing to library ‘/usr/local/lib/R/site-library’
* installing *source* package ‘Rcpp’ ...
** libs
ccache g++ -I/usr/share/R/include -DNDEBUG -I../inst/include/     -fpic  -g -O3 -Wall -pipe -pedantic -Wextra -Wno-empty-body -Wno-unused -c Date.cpp -o Date.o
ccache g++ -I/usr/share/R/include -DNDEBUG -I../inst/include/     -fpic  -g -O3 -Wall -pipe -pedantic -Wextra -Wno-empty-body -Wno-unused -c Module.cpp -o Module.o
ccache g++ -I/usr/share/R/include -DNDEBUG -I../inst/include/     -fpic  -g -O3 -Wall -pipe -pedantic -Wextra -Wno-empty-body -Wno-unused -c Rcpp_init.cpp -o Rcpp_init.o
ccache g++ -I/usr/share/R/include -DNDEBUG -I../inst/include/     -fpic  -g -O3 -Wall -pipe -pedantic -Wextra -Wno-empty-body -Wno-unused -c api.cpp -o api.o
ccache g++ -I/usr/share/R/include -DNDEBUG -I../inst/include/     -fpic  -g -O3 -Wall -pipe -pedantic -Wextra -Wno-empty-body -Wno-unused -c attributes.cpp -o attributes.o
ccache g++ -I/usr/share/R/include -DNDEBUG -I../inst/include/     -fpic  -g -O3 -Wall -pipe -pedantic -Wextra -Wno-empty-body -Wno-unused -c barrier.cpp -o barrier.o
g++ -shared -L/usr/lib/R/lib -Wl,-Bsymbolic-functions -Wl,-z,relro -o Rcpp.so Date.o Module.o Rcpp_init.o api.o attributes.o barrier.o -L/usr/lib/R/lib -lR
installing to /usr/local/lib/R/site-library/Rcpp/libs
** R
** inst
** preparing package for lazy loading
** help
*** installing help indices
** building package indices
** installing vignettes
** testing if installed package can be loaded
* DONE (Rcpp)

real    0m21.917s
user    0m21.388s
sys     0m2.304s
~/git/rcpp(master)$ ./cleanup 
~/git/rcpp(master)$ time R CMD INSTALL .
* installing to library ‘/usr/local/lib/R/site-library’
* installing *source* package ‘Rcpp’ ...
** libs
ccache g++ -I/usr/share/R/include -DNDEBUG -I../inst/include/     -fpic  -g -O3 -Wall -pipe -pedantic -Wextra -Wno-empty-body -Wno-unused -c Date.cpp -o Date.o
ccache g++ -I/usr/share/R/include -DNDEBUG -I../inst/include/     -fpic  -g -O3 -Wall -pipe -pedantic -Wextra -Wno-empty-body -Wno-unused -c Module.cpp -o Module.o
ccache g++ -I/usr/share/R/include -DNDEBUG -I../inst/include/     -fpic  -g -O3 -Wall -pipe -pedantic -Wextra -Wno-empty-body -Wno-unused -c Rcpp_init.cpp -o Rcpp_init.o
ccache g++ -I/usr/share/R/include -DNDEBUG -I../inst/include/     -fpic  -g -O3 -Wall -pipe -pedantic -Wextra -Wno-empty-body -Wno-unused -c api.cpp -o api.o
ccache g++ -I/usr/share/R/include -DNDEBUG -I../inst/include/     -fpic  -g -O3 -Wall -pipe -pedantic -Wextra -Wno-empty-body -Wno-unused -c attributes.cpp -o attributes.o
ccache g++ -I/usr/share/R/include -DNDEBUG -I../inst/include/     -fpic  -g -O3 -Wall -pipe -pedantic -Wextra -Wno-empty-body -Wno-unused -c barrier.cpp -o barrier.o
g++ -shared -L/usr/lib/R/lib -Wl,-Bsymbolic-functions -Wl,-z,relro -o Rcpp.so Date.o Module.o Rcpp_init.o api.o attributes.o barrier.o -L/usr/lib/R/lib -lR
installing to /usr/local/lib/R/site-library/Rcpp/libs
** R
** inst
** preparing package for lazy loading
** help
*** installing help indices
** building package indices
** installing vignettes
** testing if installed package can be loaded
* DONE (Rcpp)

real    0m1.444s
user    0m1.380s
sys     0m1.452s
~/git/rcpp(master)$ 

几年后编辑:我做了 write that blog post ,它包含另一个重要的金 block 。一个人真的需要一个文件 ~/.ccache/ccache.conf 因为 a) R 新鲜地解压文件(所以'较新的 ctime)并将它们放入临时文件中。目录。因此,当使用 R CMD INSTALL somepkg_1.2.3.tar.gz(而不是仅仅使用源代码)时,这有助于:

max_size = 5.0G
# important for R CMD INSTALL *.tar.gz as tarballs are expanded freshly -> fresh ctime
sloppiness = include_file_ctime
# also important as the (temp.) directory name will differ
hash_dir = false

关于c++ - 在 R 中快速重复构建包,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45512586/

相关文章:

c++ - 在非 C++ 异常情况下解锁临界区

r - 如何从表格中使用 R 制作条形图?

c++ - 将 Valgrind 与 RInside 程序一起使用

c++ - 如何确保 win-builder 使用 c++11 构建我的包?

c++ - 斯坦福一体化

c++ - For Loop 通过乘法打印出 10 个值

c++ - RegisterRawInputDevices函数使用

c++ - 在虚拟机上使用 OpenCV 重建 C++ 程序

R - 基于日期列使用 group_by 进行平均计算?

r - 使用 dplyr::summarise 中的数据函数