Skip to content

[Rcpp-devel] rcpp overhead

3 messages · Kaveh Vakili, Dirk Eddelbuettel, Douglas Bates

#
Hi Steve,

Timing:

i use:

int start_s=clock();
..
..
int stop_s=clock();
cout << "time: " << (stop_s-start_s)/double(CLOCKS_PER_SEC)*1000 << endl;


Function:
Whatever it is, it's coming from these three functions (when i uncoment them, the rest of the code runs in comparable speed).

using namespace Rcpp;
using namespace Eigen;
using namespace std;
using Eigen::MatrixXf;
using Eigen::VectorXf;
using Eigen::RowVectorXf;


VectorXi SampleR(int& m,int& p){
	int i,j,n=m;
	VectorXi x(n);
	VectorXi y(p);
	x.setLinSpaced(n,0,n-1);
	VectorXf urd = VectorXf::Random(p).array().abs();
	for(i=0;i<p;i++){
		j=n*urd(i);
		y(i)=x(j);
		--n;
		x(j)=x(n);
    	}
	return y;		
}
VectorXf FindLine(MatrixXf& xSub,RowVectorXf& xSub_mean){
	int h = xSub.rows();
	int p = xSub.cols();
	VectorXi RIndex = SampleR(h,p);
	VectorXf beta(p);
	Eigen::Matrix<float,16,16>A;
	for(int i=1;i<p;i++)	A.block(i,0,1,p)=xSub.row(RIndex(i));
	A.block(0,0,1,p) = xSub_mean;
	beta = VectorXf::Ones(p);		
	beta = A.topLeftCorner(p,p).lu().solve(beta);
	beta/=beta.norm();
	return beta;
}
VectorXf OneDir(MatrixXf& x,MatrixXf& xSub,RowVectorXf& xSub_mean,int& h,
VectorXf& proj){
VectorXf beta(x.cols());
beta = FindLine(xSub,xSub_mean);
proj = ((x*beta).array()-xSub_mean.dot(beta)).square();
std::nth_element(proj.data(),proj.data()+h,proj.data()+proj.size());
return log(proj.head(h).mean());
}


What do you think --is there something geeky here?


Best,
#
On 11 March 2012 at 01:05, Kaveh Vakili wrote:
| Hi Steve,
| 
| Timing:
| 
| i use:
| 
| int start_s=clock();
| ..
| ..
| int stop_s=clock();
| cout << "time: " << (stop_s-start_s)/double(CLOCKS_PER_SEC)*1000 << endl;

Don't take this the wrong way, but we are not here to debug or rewrite your
code for you. 

Read the list archive. There is a well-established posting standard of using
the CRAN packages inline (to prepare small, self-contained examples) and
rbenchmark (for timing and comparison).

You expect a free service here.  Allow us to expect you to at least conform
to the list standards before we spend our "copious free time" on your problem.  

Thanks,  Dirk
#
On Sat, Mar 10, 2012 at 6:05 PM, Kaveh Vakili <kaveh.vakili at ulb.ac.be> wrote:
As others have said, it is not exactly clear what you are doing here
but when you start comparing R and Eigen-based C++ code the natural
approach is to use the RcppEigen package, which also has a plugin for
inline.

I happened to post an example of the use of RcppEigen on
http://dmbates.blogspot.com today.  The example involves sampling from
a collection of multinomial distributions which seems to be related to
what you are doing.