Reputation: 426
I want to record my computing time for a for loop function. I write the function by rcpp, and use std::clock to measure the time interval. The rcpp code is as follow:
// [[Rcpp::depends(RcppArmadillo)]]
#include <RcppArmadilloExtensions/sample.h>
using namespace Rcpp;
/* tst.cpp */
// [[Rcpp::export]]
List f_time(int N,int L,double ep,arma::rowvec yy){
List out(4);double js=0;
int sbv=4;
int n=yy.n_elem;
int d=std::pow(n,0.5);
double t0= clock();
double t=t0;
arma::mat clp(n,n);clp.randn();
arma::rowvec past=arma::randn(n).t()*clp;
arma::rowvec rd(n),rw(n);
for(int i=1;i<N;i++){
arma::mat zm(L,n),zv(L,n);
zm.zeros();
zv=zv.randn()*clp;
zm.row(0)=past;
}
t = clock()-t0;
t=((float)t)/CLOCKS_PER_SEC;
out(3)=t;
return(out);
}
I tun the R code as follow
Rcpp::sourceCpp('tst.cpp')
t1<-Sys.time()
res=f_time(1e3,10,0.1,rnorm(625))
t2<-Sys.time()
t2-t1
print(res[[4]])
The output is
But the time interval in R(t2-t1) is different from the time interval calculated in rcpp(res[[4]]), how can I get the same result as R's Sys.time calculation in my cpp function?
Upvotes: 1
Views: 322