Movatterモバイル変換


[0]ホーム

URL:


rco - The R Code Optimizer

CRAN statusLifecycle: stableTravis build statusAppVeyor build statusCoverage status

Make your R code run faster!rco analyzes your code andapplies different optimization strategies that return an R code thatruns faster.

Therco project, from its start to version 1.0.0, wasmade possible by aGoogleSummer of Code 2019 project.

Thanks to the kind mentorship ofDr. Yihui Xie andDr. NicolásWolovick.

Installation

Install the current released version ofrco fromCRAN:

install.packages("rco")

Or install the development version from GitHub:

if (!require("remotes")) {  install.packages("remotes")}remotes::install_github("jcrodriguez1989/rco", dependencies = TRUE)

Usage

rco can be used in three ways:

Example

Suppose we have the following code:

code<-paste("# I want to know my age in seconds!","years_old <- 29","days_old <- 365 * years_old # leap years don't exist","hours_old <- 24 * days_old","seconds_old <- 60 * 60 * hours_old","","if (seconds_old > 10e6) {",'  print("Whoa! More than a million seconds old, what a wise man!")',"} else {",'  print("Meh!")',"}",sep ="\n")

We can automatically optimize it by doing:

opt_code<-optimize_text(code,iterations =1)
## Optimization number 1## # I want to know my age in seconds!## years_old <- 29## days_old <- 365 * 29 # leap years don't exist## hours_old <- 24 * days_old## seconds_old <- 3600 * hours_old## ## if (seconds_old > 10e6) {##   print("Whoa! More than a million seconds old, what a wise man!")## } else {##   print("Meh!")## }

After one optimization pass we can see that it has only propagatedtheyears_old variable. Another pass:

opt_code<-optimize_text(opt_code,iterations =1)
## Optimization number 1## # I want to know my age in seconds!## years_old <- 29## days_old <- 10585 # leap years don't exist## hours_old <- 24 * 10585## seconds_old <- 3600 * hours_old## ## if (seconds_old > 10e6) {##   print("Whoa! More than a million seconds old, what a wise man!")## } else {##   print("Meh!")## }

Now, it has folded thedays_old variable, and thenpropagated it. Another pass:

opt_code<-optimize_text(opt_code,iterations =1)
## Optimization number 1## # I want to know my age in seconds!## years_old <- 29## days_old <- 10585 # leap years don't exist## hours_old <- 254040## seconds_old <- 3600 * 254040## ## if (seconds_old > 10e6) {##   print("Whoa! More than a million seconds old, what a wise man!")## } else {##   print("Meh!")## }

It has folded thehours_old variable, and thenpropagated it. Another pass:

opt_code<-optimize_text(opt_code,iterations =1)
## Optimization number 1## # I want to know my age in seconds!## years_old <- 29## days_old <- 10585 # leap years don't exist## hours_old <- 254040## seconds_old <- 914544000## ## if (914544000 > 10e6) {##   print("Whoa! More than a million seconds old, what a wise man!")## } else {##   print("Meh!")## }

It has folded theseconds_old variable, and thenpropagated it into theif condition. Another pass:

opt_code<-optimize_text(opt_code,iterations =1)
## Optimization number 1## # I want to know my age in seconds!## years_old <- 29## days_old <- 10585 # leap years don't exist## hours_old <- 254040## seconds_old <- 914544000## ## print("Whoa! More than a million seconds old, what a wise man!")

Now, it has folded theif condition, and as it wasTRUE it just kept its body, as testing the condition or theelse clause were dead code. So,optimize_textfunction has automatically detected constant variables, constantfoldable operations, and dead code. And returned an optimized Rcode.

Guidelines for contributing

rco is an open source package, and the contributions tothe development of the library are more than welcome. Please see ourCONTRIBUTING.mdfile and“Contributingan Optimizer” article for detailed guidelines of how tocontribute.

Code of Conduct

Please note that the ‘rco’ project is released with aContributorCode of Conduct.

By contributing to this project, you agree to abide by its terms.


[8]ページ先頭

©2009-2025 Movatter.jp