Skip to contents
library(tinycodet)
#> Run `?tinycodet::tinycodet` to open the introduction help page of 'tinycodet'.
loadNamespace("bench")
#> <environment: namespace:bench>
loadNamespace("ggplot2")
#> <environment: namespace:ggplot2>

 

Introduction

Although the functions in this R package were written mostly in ‘R’ (and a little bit in ‘C’ and ‘C++’), the functions - especially the string related ones - have been well optimized. The string related functions, for example, are about in the same order of magnitude in terms of speed as the stringi functions they call.

Here some speed comparisons are given, using the ‘bench’ package.

 

stri_locate_ith

n <- 1e5
x <- rep(paste0(1:50, collapse = ""), n)
p <- "\\d"
i <- sample(c(-50:-1, 1:50), replace=TRUE, size = n)
locate_stringi <- function(...) {
  stringi::stri_locate_all(...)
  stringi::stri_count(...)
}
bm.stri_locate_ith <- bench::mark(
  "stri_locate_ith" = { stri_locate_ith_regex(x, p, i) },
  "stringi::(stri_locate_all + stri_count)" = { locate_stringi(str=x, regex = p) },
  min_iterations = 500,
  filter_gc = FALSE,
  check = FALSE
)
ggplot2::autoplot(bm.stri_locate_ith)
#> Loading required namespace: tidyr

 

strcut

n <- 1e5
x <- rep("hello", n)
i <- sample(1:3, n, replace = TRUE)
loc <- stri_locate_ith(x, i=i, regex="a|e|i|o|u")
bm.strcut <- bench::mark(
  "strcut_loc" = { strcut_loc(x, loc) },
  "strcut_brk" = { strcut_brk(x, type = "", tolist = TRUE) },
  "stringi::stri_split_boundaries" = {
    stringi::stri_split_boundaries(x, type="character")
  },
  min_iterations = 500,
  check = FALSE,
  filter_gc = FALSE
)
ggplot2::autoplot(bm.strcut)

 

Row/columns-wise re-ordering

tempfun <- function(x, margin) {
  if(margin == 1) {
    for(i in 1:nrow(x)) x[i,] <- sort(x[i,])
    return(x)
  }
  if(margin == 2) {
    for(i in 1:ncol(x)) x[,i] <- sort(x[,i])
    return(x)
  }
}
mat <- matrix(sample(seq_len(n^2)), ncol = n)
bm.roworder <- bench::mark(
  tinycodet = mat %row~% mat,
  "base R" = tempfun(mat, 1),
  min_iterations = 250
)
ggplot2::autoplot(bm.roworder)

bm.colorder <- bench::mark(
  tinycodet = mat %col~% mat,
  "base R" = tempfun(mat, 2),
  min_iterations = 250
)
ggplot2::autoplot(bm.colorder)