Advertisement
SteveWeston

Sequential for loop benchmark V2

Sep 16th, 2013
365
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
R 0.62 KB | None | 0 0
  1. args <- commandArgs(trailingOnly=TRUE)
  2. n <- if (length(args) > 0) as.integer(args[1]) else 1000000
  3. m <- if (length(args) > 1) as.integer(args[2]) else ceiling(n / 100)
  4. set.seed(107)
  5. td <- data.frame(val=rnorm(n), id=sample(m, n, replace=TRUE))
  6.  
  7. start <- proc.time()[3]
  8. res <- rep(0, NROW(td))
  9. for (i in unique(td$id))
  10.   res[td$id == i] <- mean(td$val[td$id != i])
  11. elapsed <- proc.time()[3] - start
  12.  
  13. cat("Sequential for loop:\n")
  14. cat(sprintf("Rows: %d, Unique IDs: %d\n", length(td$id), length(unique(td$id))))
  15. cat(sprintf("Elapsed time: %f\n", elapsed))
  16.  
  17. library(digest)
  18. cat(sprintf("MD5 hash: %s\n", digest(res)))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement