From 8762cc0986cf9a605149d6e82d8e22da7bbcf846 Mon Sep 17 00:00:00 2001 From: Toby Dylan Hocking Date: Fri, 20 Sep 2024 13:16:48 -0400 Subject: [PATCH] update examples --- man/atime.Rd | 42 +++++++++----------------------------- man/references_best.Rd | 46 +++++++++++++++++++++++++++++++++--------- 2 files changed, 47 insertions(+), 41 deletions(-) diff --git a/man/atime.Rd b/man/atime.Rd index f213cd0..a436891 100644 --- a/man/atime.Rd +++ b/man/atime.Rd @@ -36,47 +36,25 @@ result=FALSE, ...)} \examples{ ## Example 1: polynomial and exponential time string functions. -string.result <- atime::atime( +atime_result_string <- atime::atime( + seconds.limit=0.001, N=unique(as.integer(10^seq(0,3.5,l=100))), setup={ subject <- paste(rep("a", N), collapse="") pattern <- paste(rep(c("a?", "a"), each=N), collapse="") + linear_size_replacement <- paste(rep("REPLACEMENT", N), collapse="") }, - seconds.limit=0.001, PCRE.match=regexpr(pattern, subject, perl=TRUE), TRE.match=regexpr(pattern, subject, perl=FALSE), constant.replacement=gsub("a","constant size replacement",subject), - linear.replacement=gsub("a",subject,subject)) -plot(string.result) + linear.replacement=gsub("a",linear_size_replacement,subject)) +plot(atime_result_string) -## Example 2: split data table vs frame, constant factor difference. -library(data.table) -split.result <- atime::atime( - setup={ - set.seed(1) - DT <- data.table( - x1 = rep(c("c","d"), l=N), - x2 = rep(c("x","y"), l=N), - x3 = rep(c("a","b"), l=N), - y = rnorm(N) - )[sample(.N)] - DF <- as.data.frame(DT) - }, +## Example 2: combine using rbind inside or outside for loop. +atime_result_rbind <- atime::atime( seconds.limit=0.001, - frame=split(DF[names(DF) != "x1"], DF["x1"], drop = TRUE), - table=split(DT, by = "x1", keep.by = FALSE, drop = TRUE) -) -plot(split.result) - -## Example 3: combine inside or outside for loop. -atime.list <- atime::atime( - N=as.integer(10^seq(0, 5), by=0.25), setup={ - Nrow <- 1e3 - DF <- data.frame( - x1 = rep(c("c","d"), l=Nrow), - x2 = rep(c("x","y"), l=Nrow), - x3 = rep(c("a","b"), l=Nrow)) + DF <- data.frame(i=1:100) }, inside={ big.frame <- data.frame() @@ -89,9 +67,9 @@ atime.list <- atime::atime( for(table.i in 1:N){ big.frame.list[[table.i]] <- DF } - do.call(rbind, big.frame.list) + big.frame <- do.call(rbind, big.frame.list) } ) -plot(atime.list) +plot(atime_result_rbind) } diff --git a/man/references_best.Rd b/man/references_best.Rd index cabf070..0ed3004 100644 --- a/man/references_best.Rd +++ b/man/references_best.Rd @@ -26,26 +26,54 @@ references_best(L, fun.list=NULL) \examples{ ## Example 1: polynomial and exponential time string functions. -string.result <- atime::atime( +atime_result_string <- atime::atime( + seconds.limit=0.001, N=unique(as.integer(10^seq(0,4,l=100))), setup={ subject <- paste(rep("a", N), collapse="") pattern <- paste(rep(c("a?", "a"), each=N), collapse="") + linear_size_replacement <- paste(rep("REPLACEMENT", N), collapse="") }, - seconds.limit=0.001, PCRE.match=regexpr(pattern, subject, perl=TRUE), TRE.match=regexpr(pattern, subject, perl=FALSE), constant.replacement=gsub("a","constant size replacement",subject), - linear.replacement=gsub("a",subject,subject)) -(string.best <- atime::references_best(string.result)) + linear.replacement=gsub("a",linear_size_replacement,subject)) +(refs_best_string <- atime::references_best(atime_result_string)) ## plot method shows each expr and unit in a separate panel. ## default is to show closest larger and smaller references. -plot(string.best) +plot(refs_best_string) ## modifying plot.references changes violet references shown by plot. -string.best$plot.references <- string.best$ref[c("N","N^2","N^3","2^N"),on="fun.name"] -plot(string.best) +refs_best_string$plot.references <- refs_best_string$ref[c("N","N^2","N^3","2^N"),on="fun.name"] +plot(refs_best_string) ## predict method computes N for given units (default seconds limit). -string.pred = predict(string.best) -plot(string.pred) +(pred_string <- predict(refs_best_string)) +plot(pred_string) + +## Example 2: combine using rbind inside or outside for loop. +atime_result_rbind <- atime::atime( + seconds.limit=0.001, + setup={ + DF <- data.frame(i=1:100) + }, + inside={ + big.frame <- data.frame() + for(table.i in 1:N){ + big.frame <- rbind(big.frame, DF) + } + }, + outside={ + big.frame.list <- list() + for(table.i in 1:N){ + big.frame.list[[table.i]] <- DF + } + big.frame <- do.call(rbind, big.frame.list) + } +) +(refs_best_rbind <- atime::references_best(atime_result_rbind)) +plot(refs_best_rbind) +refs_best_rbind$plot.references <- refs_best_rbind$ref[c("N","N^2"),on="fun.name"] +plot(refs_best_rbind) +(pred_rbind <- predict(refs_best_rbind)) +plot(pred_rbind) }