If you want to give the dataframe a different name, you can pass that name to the constructor:
Rcpp::cppFunction('
double demo_rnorm()
{
Rcpp::Timer timer("mytimes");
timer.tic();
double x = rnorm(1, 1)[0];
timer.toc();
return(x);
}',
depends = "rcpptimer"
)
demo_rnorm()
## [1] 1.156661
## Microseconds SD Min Max Count
## tictoc 5.962 0 5.962 5.962 1
You may need to handle the resulting DataFrame yourself instead of
letting rcpptimer
write it to the global environment.
This issue consists of two elements:
autoreturn
featureFirst, to turn off autoreturn
, set the
autoreturn
variable of your Timer
instance to
false
. That will prevent the timer
from
writing the results to the global environment.
Rcpp::cppFunction('
double demo_rnorm()
{
Rcpp::Timer timer;
timer.autoreturn = false;
timer.tic("rnorm");
double x = rnorm(1, 1)[0];
timer.toc("rnorm");
return(x);
}',
depends = "rcpptimer"
)
demo_rnorm()
## [1] -0.01127144
## [1] "demo_rnorm"
Now, mem
will not write to the global environment.
However, you can still access the results through the timer’s
.stop()
method:
Rcpp::cppFunction('
DataFrame demo_rnorm()
{
Rcpp::Timer timer;
timer.autoreturn = false;
timer.tic("rnorm");
double x = rnorm(1, 1)[0];
timer.toc("rnorm");
DataFrame times = timer.stop();
return(times);
}',
depends = "rcpptimer"
)
demo_rnorm()
## Microseconds SD Min Max Count
## rnorm 5.73 0 5.73 5.73 1
It is also possible to use .stop()
and let
rcpptimer pass the results automatically. In the above example, just set
autoreturn
to true (the default) or delete that line.
You can find instructions in vignette("advanced")
if you
want to access the raw timings data.