[R] Computing time when calling C functions - why does an extra function call induce such an overhead?
Søren Højsgaard
Soren.Hojsgaard at agrsci.dk
Thu Apr 10 02:20:54 CEST 2008
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);
}
More information about the R-help
mailing list