我只是想检查R与Rcpp中Fiboncci数生成的执行速度.令我惊讶的是,我的R功能比我的Rcpp功能更快(也是线性增长).这里有什么问题.
R代码:
fibo = function (n){
x = rep(0, n)
x[1] = 1
x[2] = 2
for(i in 3:n){
x[i] = x[i-2] + x[i-1]
}
return(x)
}
Run Code Online (Sandbox Code Playgroud)
Rcpp代码:
#include <Rcpp.h>
using namespace Rcpp;
// [[Rcpp::export]]
IntegerVector fibo_sam(int n){
IntegerVector x;
x.push_back(1);
x.push_back(2);
for(int i =2; i < n; i++){
x.push_back(x[i - 2] + x[i-1]);
}
return(x);
}
Run Code Online (Sandbox Code Playgroud)