Can I use a list as a hash in R? If so, why is it so slow?

前端 未结 7 801
遇见更好的自我
遇见更好的自我 2020-11-29 23:43

Before using R, I used quite a bit of Perl. In Perl, I would often use hashes, and lookups of hashes are generally regarded as fast in Perl.

For example, the followi

7条回答
  •  旧时难觅i
    2020-11-30 00:15

    First off, as Vince and Dirk has said, you are not using hashes in your example code. A literal translation of the perl example would be

    #!/usr/bin/Rscript
    testHash <- new.env(hash = TRUE, size = 10000L)
    for(i in 1:10000) {
      key <- paste(sample(letters, 3, replace = TRUE), collapse = "")
      assign(key, floor(1000*runif(1)), envir = testHash)
    }
    
    keyArray <- ls(envir = testHash)
    keyLen <- length(keyArray)
    
    for(j in 1:10000) {
      key <- keyArray[sample(keyLen, 1)]
      lookupValue <- get(key, envir = testHash)
      cat(paste("key", key, "Lookup", lookupValue, "\n"))
    }
    

    which runs plenty fast on my machine, them main time being the setup. (Try it and post the timings.)

    But the real problem, as John said, is that you have to think vectors in R (like map in perl) and his solution is probably the best. If you do want to use hashes, consider

    keys <- sample(ls(envir = testHash), 10000, replace = TRUE)
    vals <- mget(keys, envir = testHash)
    

    after the same setup as above, which is near-instantaneous on my machine. To print them all try

    cat(paste(keys, vals), sep="\n")
    

    Hope this helps a little.

    Allan

提交回复
热议问题