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

Alternative lambda expression mechanism #23

Closed
renkun-ken opened this issue Jul 6, 2014 · 2 comments
Closed

Alternative lambda expression mechanism #23

renkun-ken opened this issue Jul 6, 2014 · 2 comments

Comments

@renkun-ken
Copy link
Owner

do.call(Map,c(function(...) list(...), lst, .i=list(seq_along(lst)), .name=list(names(lst))))

An alternative method of constructing lambda expression symbols is to construct the argument list for Map in the first place instead of constructing environment in each iteration.

@renkun-ken
Copy link
Owner Author

list.map2 <- function(.data,expr) {
  expr <- substitute(expr)
  l <- lambda(expr)
  envir <- new.env(FALSE,parent.frame())
  args <- c(function(...) {
    args <- list(...)
    list2env(args,envir)
    eval(l$expr,args$.,envir)
  },list(.data),list(seq_along(.data)),list(names(.data)))
  names(args) <- c("f",l$symbols)
  do.call(Map, args)
}

Above is an example of the method but benchmark does not show significant performance improvement.

> microbenchmark::microbenchmark(a=list.map(devs,age+.i),b=list.map2(devs,age+.i),times = 10000)
Unit: microseconds
 expr    min     lq median     uq      max neval
    a 46.390 50.085 51.727 53.370  1071.07 10000
    b 42.285 45.980 47.621 49.264 41228.14 10000

@renkun-ken
Copy link
Owner Author

> microbenchmark::microbenchmark(a=list.map(users,age+.i),b=list.map2(users,age+.i),times = 10)
Unit: milliseconds
 expr      min       lq   median       uq      max neval
    a 515.8601 559.6479 568.8228 577.3335 611.1610    10
    b 425.9984 451.1198 460.6385 481.0625 669.6938    10

where users is a list of 50,000 records. The memory usage is very efficient.

The performance seems to improve 20%.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant