RcppClock | R Documentation |
Time Rcpp functions and summarize, print, and plot runtime statistics
## S3 method for class 'RcppClock' summary(object, units = "auto", ...) ## S3 method for class 'RcppClock' print(x, ...) ## S3 method for class 'RcppClock' plot(x, ...)
object |
RcppClock object |
units |
nanoseconds ( |
... |
arguments to other functions |
x |
RcppClock object |
See https://github.com/zdebruine/RcppClock/readme.md for information on how to use the package.
See the RcppClock README on https://github.com/zdebruine/RcppClock#readme
for basic usage examples.
When the Rcpp Rcpp::clock::stop()
method is called in Rcpp code, an S3 RcppClock
object
will be created in the global environment. This object contains three methods:
summary
: computes runtime summary statistics and returns a data.frame
print
: runs summary
and then prints the resulting data.frame
plot
: a ggplot2 violin plot with jitter points showing runtimes for each expression
The fibonacci
function is a simple example of how to use RcppClock.
See the source code on github.com/zdebruine/RcppClock/src/fibonacci.cpp
fibonacci
library(RcppClock) fibonacci(n = 25:35, reps = 10) # this function creates a global environment variable "clock" # that is an S3 RcppClock object clock plot(clock) summary(clock, units = "ms") ## Not run: # this is the Rcpp code behind the "fibonacci" example function ```{Rcpp} //[[Rcpp::depends(RcppClock)]] #include <RcppClock.h> int fib(int n) { return ((n <= 1) ? n : fib(n - 1) + fib(n - 2)); } //[[Rcpp::export]] void fibonacci(std::vector<int> n, int reps = 10) { Rcpp::Clock clock; while(reps-- > 0){ for(auto number : n){ clock.tick("fib" + std::to_string(number)); fib(number); clock.tock("fib" + std::to_string(number)); } } clock.stop("clock"); } ``` ## End(Not run)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.