Most efficient way to loop through '...'

ThomasP85

I'm developing a package in R which main interface is two classes: a class handling data (let's call it Data) and a class handling a collection of Data instances (we call it Collection). Data instances can be rather larger (50 mb +), which is why the Collection class is a wrapper around an environment.

I want to implement a c() function for Data which will result in a Collection instance. In order to do so the arguments are passed as ... and the Collection instance has to be build up iteratively. I know I can loop through ... by first making it a list by list(...), but this could be potentially expensive. Is there anyway to loop through ... without first copying everything over to a temporary list?

Jan van der Laan

What seems to work is to use the dots function from the pryr package (I can imagine there are other more simple solutions possible):

library(pryr)

fun1 <- function(...) {
  l <- dots(...)
  result <- list(mode="list", length = length(l))
  for (i in seq_along(l)) {
    v <- l[[i]]
    result[[i]] <- eval(v)
  }
   result
}

Timing this against an implementation using list and an implementation without ...

fun2 <- function(...) {
  l <- list(...)
  result <- list(mode="list", length = length(l))
  for (i in seq_along(l)) {
    v <- l[[i]]
    result[[i]] <- v
  }
  result
}

fun1 <- function(...) {
  l <- dots(...)
  result <- list(mode="list", length = length(l))
  for (i in seq_along(l)) {
    v <- l[[i]]
    result[[i]] <- eval(v)
  }
  result
}

fun3 <- function(a, b) {
  list(a, b)
}

> system.time(r1 <- fun1(large_object1, large_object2))
   user  system elapsed 
  0.060   0.072   0.133 
> system.time(r2 <- fun2(large_object1, large_object2))
   user  system elapsed 
  0.132   0.132   0.265 
> system.time(r3 <- fun3(large_object1, large_object2))
   user  system elapsed 
  0.056   0.076   0.132 

We see that the implementation using dots performs similar as the implementation without ... and faster than the implementation using list.

If you don't want to depend on pryr: the code of dots is quite 'simple':

dots <- function (...) {
  eval(substitute(alist(...)))
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

most efficient way to loop through log records

From Dev

What is the most efficient way to loop through picture pixels

From Dev

Most efficient way to communicate through wifi

From Dev

Most efficient way to loop through multiple csv files and calculate NYSE tick

From Dev

Java: Most efficient way to loop through CSV and sum values of one column for each unique value in another Column

From Dev

What is the fastest/most efficient way to loop through a large collection of files and save a plot of the data?

From Dev

Most efficient way in terms of speed and memory to iterate through a very large loop and store a scipy sparse matrix to a file

From Java

Efficient way to loop through GroupBy DataFrame

From Dev

Efficient way to use to loop through the list in Java

From Dev

Most efficient way to search through an object and array locating match

From Dev

Looping through Pandas dataframe to generate list - most efficient way

From Dev

Most efficient way of continuously going through an array of nth length?

From Dev

Most efficient way to "clip" a value to repeat through a range?

From Dev

What is the most efficient way to clip a video running through canvas

From Dev

Most efficient way of searching and adding to a large list that grows with every loop

From Dev

Most efficient way to loop finding items not in a list in python

From Dev

Most efficient way to implement this?

From Dev

Most efficient way to execute this

From Dev

Efficient way to loop through pixels of 16-bit Mat in OpenCV

From Dev

Most efficient way to compute a polynomial

From Dev

query with calculations most efficient way

From Dev

Most efficient way to split sentence

From Dev

Most efficient way to rename an image

From Dev

Most efficient way to output a newline

From Dev

Most efficient way to determine an intersection

From Dev

Most efficient way to count occurrences?

From Dev

Most efficient way to concatenate Strings

From Dev

Most efficient way to compute a polynomial

From Dev

Most efficient way to store this data

Related Related

  1. 1

    most efficient way to loop through log records

  2. 2

    What is the most efficient way to loop through picture pixels

  3. 3

    Most efficient way to communicate through wifi

  4. 4

    Most efficient way to loop through multiple csv files and calculate NYSE tick

  5. 5

    Java: Most efficient way to loop through CSV and sum values of one column for each unique value in another Column

  6. 6

    What is the fastest/most efficient way to loop through a large collection of files and save a plot of the data?

  7. 7

    Most efficient way in terms of speed and memory to iterate through a very large loop and store a scipy sparse matrix to a file

  8. 8

    Efficient way to loop through GroupBy DataFrame

  9. 9

    Efficient way to use to loop through the list in Java

  10. 10

    Most efficient way to search through an object and array locating match

  11. 11

    Looping through Pandas dataframe to generate list - most efficient way

  12. 12

    Most efficient way of continuously going through an array of nth length?

  13. 13

    Most efficient way to "clip" a value to repeat through a range?

  14. 14

    What is the most efficient way to clip a video running through canvas

  15. 15

    Most efficient way of searching and adding to a large list that grows with every loop

  16. 16

    Most efficient way to loop finding items not in a list in python

  17. 17

    Most efficient way to implement this?

  18. 18

    Most efficient way to execute this

  19. 19

    Efficient way to loop through pixels of 16-bit Mat in OpenCV

  20. 20

    Most efficient way to compute a polynomial

  21. 21

    query with calculations most efficient way

  22. 22

    Most efficient way to split sentence

  23. 23

    Most efficient way to rename an image

  24. 24

    Most efficient way to output a newline

  25. 25

    Most efficient way to determine an intersection

  26. 26

    Most efficient way to count occurrences?

  27. 27

    Most efficient way to concatenate Strings

  28. 28

    Most efficient way to compute a polynomial

  29. 29

    Most efficient way to store this data

HotTag

Archive