Profiling Rcpp code on OS X

后端 未结 1 486
Happy的楠姐
Happy的楠姐 2020-12-24 12:26

I am interested in profiling some Rcpp code under OS X (Mountain Lion 10.8.2), but the profiler crashes when being run.

Toy example, using inline, just

1条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-24 13:08

    I'm confused, your example is incomplete:

    • you don't show the (trivial) invocation of cfunction() and cxxfunction()

    • you don't show how you invoke the profiler

    • you aren't profiling the C or C++ code (!!)

    Can you maybe edit the question and make it clearer?

    Also, when I run this, the two example do give identical speed results as they are essentially identical. [ Rcpp would let you do this in call with sugars random number functions. ]

    R> library(Rcpp)
    R> library(inline)
    R> 
    R> src.cpp <- "
    +   RNGScope scope;
    +   int n = as(n_);
    +   double x = 0.0;
    +   for ( int i = 0; i < n; i++ )
    +     x += (unif_rand()-.5);
    +   return wrap(x);"
    R> 
    R> src.c <- "
    +   int i, n = INTEGER(n_)[0];
    +   double x = 0.0;
    +   GetRNGstate();
    +   for ( i = 0; i < n; i++ )
    +     x += (unif_rand()-.5);
    +   PutRNGstate();
    +   return Rf_ScalarReal(x);"
    R> 
    R> fc   <- cfunction(signature(n_="int"), body=src.c)
    R> fcpp <- cxxfunction(signature(n_="int"), body=src.c, plugin="Rcpp")
    R> 
    R> library(rbenchmark)
    R> 
    R> print(benchmark(fc(10000L), fcpp(10000L)))
             test replications elapsed relative user.self sys.self user.child sys.child
    1   fc(10000)          100   0.013        1     0.012        0          0         0
    2 fcpp(10000)          100   0.013        1     0.012        0          0         0
    R> 
    

    0 讨论(0)
提交回复
热议问题