[R] Computing time when calling C functions - why does an extra function call induce such an overhead?

From: Søren Højsgaard <Soren.Hojsgaard_at_agrsci.dk>
Date: Thu, 10 Apr 2008 02:20:54 +0200


Dear list,  

I am a little puzzled by computing time in connection with calling C functions. With the function mysolve1 given below I solve Ax=B, where the actual matrix operation takes place in mysolve2. Doing this 5000 times takes 3.51 secs. However, if I move the actual matrix inversion part into mysolve1 (by uncommenting the two commented lines and skip the call to mysolve2) then the computations take only 0.03 secs. Can anyone tell me why there is such a "time-overhead" in introducing the extra function call to mysolve2?  

I am on windows XP using R 2.6.1  

Best regards
Søren  

SEXP mysolve1(SEXP Ain, SEXP Bin, SEXP tolin)
{

  int *Adims, *Bdims;
  double tol = asReal(tolin);
  SEXP A, B;
  PROTECT(A = duplicate(Ain));
  PROTECT(B = duplicate(Bin));
  Adims = INTEGER(coerceVector(getAttrib(A, R_DimSymbol), INTSXP));   Bdims = INTEGER(coerceVector(getAttrib(B, R_DimSymbol), INTSXP));   int nrA, ncB;
  double *Ap, *Bp;

  nrA     = Adims[0];
  ncB     = Bdims[1];
  Ap      = REAL(A);
  Bp      = REAL(B);

/* int info, *ipiv = (int *) R_alloc(nrA, sizeof(int)); */ /* F77_CALL(dgesv)(&nrA, &ncB, Ap, &nrA, ipiv, Bp, &nrA, &info); */   mysolve2(Ap, &nrA, Bp, &ncB, &tol);
  UNPROTECT(2);
  return B;
}  

void mysolve2(double *A, int *nrA, double *B, int *ncB, double *tolin)
{

  int info;

  int *ipiv = (int *) R_alloc((int) nrA, sizeof(int));   F77_CALL(dgesv)(nrA, ncB, A, nrA, ipiv, B, nrA, &info); }



R-help_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. Received on Thu 10 Apr 2008 - 00:27:39 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Sun 13 Apr 2008 - 14:30:28 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive