[Rd] Why is the diag function so slow (for extraction)?

Martin Maechler maechler at lynne.stat.math.ethz.ch
Tue May 12 14:33:48 CEST 2015


>>>>> Steve Bronder <sbronder at stevebronder.com>
>>>>>     on Thu, 7 May 2015 11:49:49 -0400 writes:

    > Is it possible to replace c() with .subset()? 

It would be possible, but I think "entirely" wrong.

.subset() is documented to be an internal function not to be
used "lightly" and more to the point it is documented to *NOT*
dispatch at all.

If you read and understood what Peter and Luke wrote, you'd not
special case here:

diag() should not work only for pure matrices, but for all
"matrix-like" objects for which ``the usual methods'' work, such
as
   as.vector(.), c(.)

That's why there has been the c(.) in there.

You can always make code faster if you write the code so it only
has to work in one special case .. and work there very fast.


    > Example below
    > ####
    > ####

    > library(microbenchmark)



More information about the R-devel mailing list