Skip to content
Prev 37813 / 63421 Next

Is it safe not to coerce matrices with as.double() in .C()?

From: Liaw, Andy
My apologies for coming back to this so late.  I did some test, and found that

  storage.mode(x) <- "double"

isn't as low on resource as I thought it might be.  Changing the code to this from

  x <- as.double(x)

did not give the expected speed improvement.  Here's a little test:

f1 <- function(x) { as.double(x); NULL }
f2 <- function(x) { storage.mode(x) <- "double"; NULL }
f3 <- function(x) { x <- x; NULL }
set.seed(917)
reps <- 500
x <- matrix(rnorm(1e6L), 1e3L, 1e3L)
system.time(junk <- replicate(reps, f1(x)))
system.time(junk <- replicate(reps, f2(x)))
system.time(junk <- replicate(reps, f3(x)))

On my laptop running R  2.11.1 Patched (2010-06-26 r52410), I get:

R> system.time(junk <- replicate(reps, f1(x)))
   user  system elapsed 
   3.54    2.14    5.74 
R> system.time(junk <- replicate(reps, f2(x)))
   user  system elapsed 
   3.32    2.11    5.92 
R> system.time(junk <- replicate(reps, f3(x)))
   user  system elapsed 
      0       0       0 

Perhaps I need to first check and see if the storage mode is as expected before trying coercion?

Best,
Andy
Notice:  This e-mail message, together with any attachments, contains
information of Merck & Co., Inc. (One Merck Drive, Whitehouse Station,
New Jersey, USA 08889), and/or its affiliates Direct contact information
for affiliates is available at 
http://www.merck.com/contact/contacts.html) that may be confidential,
proprietary copyrighted and/or legally privileged. It is intended solely
for the use of the individual or entity named on this message. If you are
not the intended recipient, and have received this message in error,
please notify us immediately by reply e-mail and then delete it from 
your system.