Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

error for N not unique #72

Merged
merged 2 commits into from
Dec 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading