@heavywatal
r_for = function(n) {
s = 0; for (i in seq_len(n)) {s = s + 1 / i}; s
}
r_vec = function(n) sum(1 / seq_len(n))
Rcpp::cppFunction("double rcpp(int n) {
double s = 0; for (int i = 1; i <= n; ++i) {s += 1.0 / i;} return s;
}") # Compilation takes a few seconds here
n = 1000000L
rbenchmark::benchmark(r_for(n), r_vec(n), rcpp(n))[,1:4]
test replications elapsed relative
1 r_for(n) 100 3.968 29.835
2 r_vec(n) 100 0.473 3.556
3 rcpp(n) 100 0.133 1.000
⚡ Rcpp helps improving performance.
📦 ~1500 CRAN packages depend on Rcpp.
🎉 Happy 10th birthday and version 1.0.0 (2018-11-05).
src/
) - R packages by Hadley Wickhamこの内容が無料で読めるなんてすごい。 https://t.co/6I7GGemCDZ
— HOXOM Inc. (@hoxom_inc) October 30, 2018
この内容が無料で読めるなんてすごい https://t.co/0CRbCrO06B
— 職業、イケメン。テラモナギ (@teramonagi) October 30, 2018
この内容が無料で読めるなんてすごい。 https://t.co/3M7Sy6tAw7
— niszet📚技術書典5 い04でした (@niszet0) October 30, 2018
この内容が無料で読めるなんてすごい。 https://t.co/1EH6CUVBmD
— hoxo_m (@hoxo_m) October 30, 2018
この内容が無料で読めるなんてすごい。 https://t.co/wtbAOgBpa8
— Shinichi Takayanagi (@_stakaya) October 30, 2018
この内容が無料で読めるなんてすごい https://t.co/VQn0YOD59i
— 凸ぽん (@kyusque) October 30, 2018
Necessity is the mother of invention. Let’s do it 💪
Source repository:
evolution/
├── README.md
└── src/
├── individual.hpp # header file for declaration
├── individual.cpp # source file for definition
├── population.hpp
└── population.cpp
Compiled and installed:
${PREFIX}/ # e.g., /usr/local/
├── include/ # header files
│ └── evolution/
│ ├── individual.hpp
│ └── population.hpp
└── lib/
└── libevolution.a # shared library (.dylib, .dll)
Libraries can be header-only, e.g., Eigen, nlohmann/json.
Write src/Makevars
to instruct compilers and linkers:
CXX_STD=CXX11
PKG_CPPFLAGS=-DSTRICT_R_HEADERS -I/usr/local/include
PKG_LIBS=-L/usr/local/lib -Wl,-rpath,/usr/local/lib -levolution
Use configure
script to
src/Makevars
from a template src/Makevars.in
.git
, cmake
, etc.inst/include/
and src/
.