[Rd] Small performance bug in [.Date

Hadley Wickham h.wickham at gmail.com
Mon Nov 20 19:50:24 CET 2017


Hi all,

I think there's an unnecessary line in [.Date which has a considerable
impact on performance when subsetting large dates:

x <- Sys.Date() + 1:1e6

microbenchmark::microbenchmark(x[1])
#> Unit: microseconds
#>  expr     min       lq     mean   median       uq      max neval
#>  x[1] 920.651 1039.346 3624.833 2294.404 3786.881 41176.38   100

`[.Date` <- function(x, ..., drop = TRUE) {
    cl <- oldClass(x)
    # class(x) <- NULL
    val <- NextMethod("[")
    class(val) <- cl
    val
}
microbenchmark::microbenchmark(x[1])
#> Unit: microseconds
#>  expr   min     lq     mean median    uq      max neval
#>  x[1] 2.738 3.0225 28.40893  3.269 3.513 2470.068   100

Setting the class of x to NULL is problematic because it forces a
copy, and I'm pretty sure it's unnecessary as NextMethod() does not
consult the class of x, but instead uses .Class.

Hadley

-- 
http://hadley.nz



More information about the R-devel mailing list