rstudio / promises

A promise library for R
https://rstudio.github.io/promises
Other
198 stars 19 forks source link

Performance should be improved #9

Open wch opened 6 years ago

wch commented 6 years ago

@jcheng5, can you supply the examples that you showed me?

jcheng5 commented 6 years ago
later_test <- function(times = 10000L) {
  start_time <- Sys.time()
  i <- 0L
  run <- function() {
    i <<- i + 1L
    if (i >= times) {
      elapsed <- Sys.time() - start_time
      print(elapsed)
    } else {
      later:::later(run, 0)
    }
  }
  later:::later(run, 0)
}

library(promises)
promise_test <- function(times = 10000L) {
  start_time <- Sys.time()
  i <- 0L
  action <- function(resolve, reject) {
    i <<- i + 1L
    if (i >= times) {
      elapsed <- Sys.time() - start_time
      print(elapsed)
      resolve(invisible(NULL))
    } else {
      f <- function() {
        p <- promise(action)
        resolve(p)
      }
      later::later(f, 0)
    }
  }
  promise(action)
}