test-code/turf-working/old-attempts/speedtest.R

make_data <- function(n, m) {
    matrix(
        data = sample(c(1, 0, NA), n * m, T, c(0.45, 0.45, 0.00)),
        nrow = n,
        ncol = m,
        dimnames = list(
            NULL, paste0("i", 1:m)
        )
    ) %>%
        as_tibble() %>%
        rowid_to_column() %>%
        add_column(weight = 1, .after = 1)
}

mm <- 20
set.seed(101)
tst <- make_data(n = 10000, m = mm)

input <-
    tibble(n = 1:mm) %>%
    mutate(keep.cols = map(n, ~paste0("i", 1:.x))) %>%
    mutate(start = 1) %>%
    rowwise() %>%
    mutate(med = floor(median(c(start, n)))) %>%
    ungroup() %>%
    mutate(
        k = map2(start, med, ~.x:.y),
        data = list(tst),
        data = map2(
            .x = data,
            .y = keep.cols,
            .f = ~select(.x, all_of(.y))
        )
    ) %>%
    select(-keep.cols, -start, -med) %>%
    unnest(k) %>%
    rowwise() %>%
    mutate(combos = ncombinations(n, k)) %>%
    ungroup() %>%
    mutate(time = NA) %>%
    group_by(n, combos) %>%
    filter(row_number() == 1) %>%
    ungroup() %>%
    arrange(desc(combos))

for (i in 1:nrow(input)) {
    a <- Sys.time()
    out <- terf(
        data = input$data[[i]],
        cols = matches("^i"),
        k = input$k[[i]],
        need = 1
    )
    b <- Sys.time()
    input$time[i] <- difftime(b, a, units = "s")
    cat("Time:", round(b - a, 3), "\n")
}

input %>%
    ggplot(aes(x = combos, y = time)) +
    geom_point() +
    geom_smooth()

lm(combos ~ time - 1, data = input)

1 / 0.0005339
ttrodrigz/onezero documentation built on May 9, 2023, 2:59 p.m.