Title: | Seamless 'Rcpp' Benchmarking |
---|---|
Description: | Time the execution of overlapping (or not) 'Rcpp' code chunks using convenient methods, seamlessly write timing results to an 'RcppClock' object in the R global environment, and summarize and/or plot the results in R using 'RcppClock' methods. |
Authors: | Zach DeBruine |
Maintainer: | Zach DeBruine <[email protected]> |
License: | GPL (>= 2) |
Version: | 1.2 |
Built: | 2024-11-04 04:04:00 UTC |
Source: | https://github.com/zdebruine/rcppclock |
Time the computation of fibonacci numbers
fibonacci(n, reps = 10L)
fibonacci(n, reps = 10L)
n |
vector giving integers for which to compute the fibonacci sum |
reps |
number of replicates for timing |
The function being timed is the following:
int fib(int n) { return ((n <= 1) ? n : fib(n - 1) + fib(n - 2)); }
Runtime for computations less than n = 25
is nearly unmeasurable.
fibonacci(n = c(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")
fibonacci(n = c(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")
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, ...)
## 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
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)
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)