ループで簡単に表現できる問題については、Rcpp が適切なソリューションであるとますます確信するようになりました。比較的簡単に習得でき、loop-y アルゴリズムを非常に自然に表現できます。
Rcppを使用した問題の解決策は次のとおりです。
#include <Rcpp.h>
using namespace Rcpp;
// [[Rcpp::export]]
List purchaseWhenPossible(NumericVector date, NumericVector income,
NumericVector price, double init_balance = 0) {
int n = date.length();
NumericVector balance(n);
LogicalVector buy(n);
for (int i = 0; i < n; ++i) {
balance[i] = ((i == 0) ? init_balance : balance[i - 1]) + income;
// Buy it if you can afford it
if (balance[i] >= price[i]) {
buy[i] = true;
balance[i] -= price[i];
} else {
buy[i] = false;
}
}
return List::create(_["buy"] = buy, _["balance"] = balance);
}
/*** R
# Copying input data from Ricardo
df <- data.frame(
dates = 1:6,
income = rep(2, 6),
price = c(5, 2, 3, 5, 2, 1)
)
out <- purchaseWhenPossible(df$dates, df$income, df$price, 3)
df$balance <- out$balance
df$buy <- out$buy
*/
実行するには、 という名前のファイルに保存してからpurchase.cpp
実行しますRcpp::sourceCpp("purchase.cpp")
C++ は非常に高速であるため、非常に高速になりますが、正式なベンチマークは行っていません。