rcpptimer

所属分类:C/C++基础
开发工具:R
文件大小:0KB
下载次数:0
上传日期:2024-02-29 17:38:09
上 传 者sh-1993
说明:  支持OpenMP的Rcpp Tic Toc计时器
(Rcpp Tic-Toc Timer with OpenMP Support)

文件列表:
R/
inst/include/
man/
src/
tests/
.Rbuildignore
DESCRIPTION
LICENSE
NAMESPACE

# RcppClock [![](https://cranlogs.r-pkg.org/badges/grand-total/RcppClock)](https://cran.r-project.org/package=RcppML) [![](https://www.r-pkg.org/badges/version-last-release/RcppClock)](https://cran.r-project.org/package=RcppML) [![License: GPL v2](https://img.shields.io/badge/License-GPL%20v2-blue.svg)](https://www.gnu.org/licenses/old-licenses/gpl-2.0.en.html) RcppClock is a simple wrapper for `std::chrono::high_resolution_clock` that makes benchmarking Rcpp code easy. Install RcppClock from CRAN. ``` install.packages("RcppClock") library(RcppClock) ?RcppClock ``` ## The Rcpp side of things Load the RcppClock header into your R session using `library(RcppClock)`, link it in your `DESCRIPTION` file or with `//[[Rcpp::depends(RcppClock)]]`, and load the header library into individual `.cpp` files with `#include `: ``` //[[Rcpp::depends(RcppClock)]] #include #include //[[Rcpp::export]] void sleepy(){ Rcpp::Clock clock; clock.tick("both_naps"); clock.tick("short_nap"); std::this_thread::sleep_for(std::chrono::milliseconds(10)); clock.tock("short_nap"); clock.tick("long_nap"); std::this_thread::sleep_for(std::chrono::milliseconds(100)); clock.tock("long_nap"); clock.tock("both_naps"); // send the times to the R global environment variable, named "naptimes" clock.stop("naptimes"); } ``` `.tick(std::string)` starts a new timer. Provide a name to record what is being timed. `.tock(std::string)` stops a timer. It is important to use the same name as declared in `.tick()`. `.stop(std::string)` calculates the duration between all `.tick()` and `.tock()` timing results, and creates an object in the R environment with the name provided. ## The R side of things On the R end, we can now do stuff with the "naptimes" variable that was created in the above Rcpp function: ```{R} sleepy() # global variable "naptimes" is now created in the environment naptimes ``` ```{R} summary(naptimes, units = "us") ``` ```{R} plot(naptimes) ``` ## Timing multiple replicates If a `.tick()` with the same name is called multiple times, RcppClock automatically groups the results. The following code reproduces the `?fibonacci` function example included in the RcppClock package: ``` int fib(int n) { return ((n <= 1) ? n : fib(n - 1) + fib(n - 2)); } //[[Rcpp::export]] void fibonacci(std::vector 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"); } ``` On the R end, we'll get an object named "clock": ```{R} fibonacci(n = 25:35, reps = 10) # global variable "clock" is created in the R global environment clock ``` ```{R} plot(clock) ``` ## Limitations * Not compatible with OpenMP parallelization. * Processes taking less than a microsecond cannot be reliably timed on some platforms.

近期下载者

相关文件


收藏者