简体   繁体   中英

Translation from Python to R. Big difference in execution time

I am translating a piece of code from Python to R to locate the position of a series of numbers when the sum duplicates. The translation is to me one-to-one, but the python code takes less than a second to execute compared to the R code which takes around an hour. I wonder if I miss anything in the data structure of both languages. Why is there such a big time difference in executing the codes? What could be a better or efficient alternative when I write the following Python code in R?

Python:

def calculate(data):
    found = set([0])
    total = 0
    while True:
        for num in data:
            total = total + num
            if total in found:
                return total
            else:
                found.add(total)

R:

calculate <- function(input) {
  found = set(0)
  total = 0
  while (TRUE) {
   for (num in input) {
     total = total + num
     if (total %in% found) {               
         return(total)
     } else {
         found <- c(found, total)
     }
    }
  }
}

at a guess this is a combination of most things being immutable in R (hence the vector concatenation will be allocating new objects every time) and R's %in% operator being O(N), compared to in being O(1) in Python

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM