CorNinaber.nl

small SD speed test in R

TODO Needs some work cleaning up


 

Was wondering which method of computing the standard deviation of different variables in a matrix would be the fastest. Thought of a few and run a timed run of all the commands.

sd.test <- function(data,method){
  switch(method,
         a = sqrt(diag(cov(data))),
         b = sqrt(diag(var(data))),
         c = sqrt(apply(data,2,var)),
         d = apply(data,2,var),
         e = apply(data,2,sd),
         f = sd(data),
         )
}

X.temp=matrix(rnorm(25000),1000,25)

for(method in letters[1:6]){
  cat(“speed analyses type:”, method, “->”,
      (system.time(for(i in 1:1000){
        sd.test(X.temp, method)
      })[3]),
      “n”)
}

speed analyses type: a -> 2.419 
speed analyses type: b -> 2.29 
speed analyses type: c -> 6.385 
speed analyses type: d -> 6.13 
speed analyses type: e -> 6.663 
speed analyses type: f -> 6.604

Appears method b is fastest, meaning the winner is…  sqrt(diag(var(data)))

Next Post

Previous Post

Leave a Reply

© 2024 CorNinaber.nl

Theme by Anders Norén