Skip to content

Commit

Permalink
Merge pull request #72 from tdhock/N_unique
Browse files Browse the repository at this point in the history
error for N not unique
  • Loading branch information
tdhock authored Dec 3, 2024
2 parents 66b632c + a1ad8a0 commit 5f722cb
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 38 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Package: atime
Type: Package
Title: Asymptotic Timing
Version: 2024.11.29
Version: 2024.12.3
Authors@R: c(
person("Toby", "Hocking",
email="[email protected]",
Expand Down
5 changes: 5 additions & 0 deletions NEWS
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
Changes in version 2024.12.3

- atime errors if user provides duplicate N values.
- predict(length=100) in sparse vignette, for CRAN.

Changes in version 2024.11.29

- use seconds.limit=Inf in test, for CRAN.
Expand Down
5 changes: 5 additions & 0 deletions R/atime.R
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,11 @@ atime <- function(N=default_N(), setup, expr.list=NULL, times=10, seconds.limit=
if(length(N)<2){
stop("length(N) should be at least 2")
}
N.tab <- table(N)
N.bad <- N.tab[N.tab>1]
if(length(N.bad)){
stop("please remove duplicate values from N: ", paste(names(N.bad), collapse=", "))
}
formal.names <- names(formals())
mc.args <- as.list(match.call()[-1])
dots.list <- mc.args[!names(mc.args) %in% formal.names]
Expand Down
2 changes: 1 addition & 1 deletion man/atime.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ N, setup, expr.list=NULL, times=10, seconds.limit=0.01, verbose=FALSE,
result=FALSE, N.env.parent=NULL, ...)}

\arguments{
\item{N}{numeric vector of at least two data sizes, default is \code{2^seq(2,20)}.}
\item{N}{numeric vector of at least two unique data sizes, default is \code{2^seq(2,20)}.}
\item{setup}{expression to evaluate for every data size, before timings.}
\item{expr.list}{named list of expressions to time.}
\item{times}{number of times to evaluate each timed expression.}
Expand Down
10 changes: 9 additions & 1 deletion tests/testthat/test-CRAN.R
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
library(data.table)
library(testthat)

test_that("warning for only one N", {
test_that("error when user provided duplicate N", {
expect_error({
atime::atime(N=c(1,2,2,3,9,9,9), num=numeric(N))
},
"please remove duplicate values from N: 2, 9",
fixed=TRUE)
})

test_that("warning when result contains only one N", {
expect_warning({
seconds.limit <- 0.001
atime.list <- atime::atime(
Expand Down
65 changes: 30 additions & 35 deletions vignettes/sparse.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ to time and memory).

```{r}
library(Matrix)
N_seq <- as.integer(10^seq(1,7,by=0.25))
N_seq <- unique(as.integer(10^seq(0,7,by=0.25)))
vec.mat.result <- atime::atime(
N=N_seq,
vector=numeric(N),
Expand Down Expand Up @@ -64,40 +64,35 @@ quantities (seconds, kilobytes, length).
seconds.limit <- 0.01
done.vec <- NULL
measure.vars <- c("seconds","kilobytes","length")
press_result <- bench::press(
N = N_seq,
{
exprs <- function(...){
as.list(match.call()[-1])
}
elist <- exprs(
vector=numeric(N),
matrix=matrix(0, N, N),
Matrix=Matrix(0, N, N))
elist[names(done.vec)] <- NA #Don't run exprs which already exceeded limit.
mark.args <- c(elist, list(iterations=10, check=FALSE))
mark.result <- do.call(bench::mark, mark.args)
## Rename some columns for easier interpretation.
desc.vec <- attr(mark.result$expression, "description")
mark.result$description <- desc.vec
mark.result$seconds <- as.numeric(mark.result$median)
mark.result$kilobytes <- as.numeric(mark.result$mem_alloc/1024)
## Compute length column to measure in addition to time/memory.
mark.result$length <- NA
for(desc.i in seq_along(desc.vec)){
description <- desc.vec[[desc.i]]
result <- eval(elist[[description]])
mark.result$length[desc.i] <- length(result)
}
## Set NA time/memory/length for exprs which were not run.
mark.result[desc.vec %in% names(done.vec), measure.vars] <- NA
## If expr went over time limit, indicate it is done.
over.limit <- mark.result$seconds > seconds.limit
over.desc <- desc.vec[is.finite(mark.result$seconds) & over.limit]
done.vec[over.desc] <<- TRUE
mark.result
press_result <- bench::press(N = N_seq, {
exprs <- function(...)as.list(match.call()[-1])
elist <- exprs(
vector=numeric(N),
matrix=matrix(0, N, N),
Matrix=Matrix(0, N, N))
elist[names(done.vec)] <- NA #Don't run exprs which already exceeded limit.
mark.args <- c(elist, list(iterations=10, check=FALSE))
mark.result <- do.call(bench::mark, mark.args)
## Rename some columns for easier interpretation.
desc.vec <- attr(mark.result$expression, "description")
mark.result$description <- desc.vec
mark.result$seconds <- as.numeric(mark.result$median)
mark.result$kilobytes <- as.numeric(mark.result$mem_alloc/1024)
## Compute length column to measure in addition to time/memory.
mark.result$length <- NA
for(desc.i in seq_along(desc.vec)){
description <- desc.vec[[desc.i]]
result <- eval(elist[[description]])
mark.result$length[desc.i] <- length(result)
}
)
## Set NA time/memory/length for exprs which were not run.
mark.result[desc.vec %in% names(done.vec), measure.vars] <- NA
## If expr went over time limit, indicate it is done.
over.limit <- mark.result$seconds > seconds.limit
over.desc <- desc.vec[is.finite(mark.result$seconds) & over.limit]
done.vec[over.desc] <<- TRUE
mark.result
})
```

The `bench::press` code above is relatively complicated, because it re-implements two functions that are provided by atime:
Expand Down Expand Up @@ -156,7 +151,7 @@ vec.mat.pred <- predict(
vec.mat.best,
seconds=vec.mat.result$seconds.limit,
##kilobytes=1000,#not available on CRAN.
length=1e6)
length=100)
plot(vec.mat.pred)
```

Expand Down

0 comments on commit 5f722cb

Please sign in to comment.