Skip to content

speed up conversion to IDateTime #1392

@jangorecki

Description

@jangorecki

Conversion of POSIXct to IDate and ITime is likely to be bottleneck. I see some place for improvement to handle POSIXct directly, not using POSIXlt as it is now.
Below some timings

install.packages("microbenchmarkCore", repos = paste0("https://",c(
    "olafmersmann.github.io/drat"
)))
library(microbenchmarkCore)
library(data.table)
x = as.POSIXct(1:1e7, origin="1970-01-01")
str(x)
# POSIXct[1:10000000], format: "1970-01-01 00:00:01" "1970-01-01 00:00:02" "1970-01-01 00:00:03" "1970-01-01 00:00:04" "1970-01-01 00:00:05" "1970-01-01 00:00:06" ...
as.ITimeNew = function(x){
    structure(as.integer(x) %% 86400, class = "ITime")
}
system.nanotime(
    r_current <- as.ITime(x)
)
#      user    system   elapsed 
#        NA        NA 0.7089193
system.nanotime(
    r_new <- as.ITimeNew(x)
)
#      user    system   elapsed 
#        NA        NA 0.2976062
all.equal(r_current, r_new)
# [1] TRUE
microbenchmark(times=5L,
               as.ITime(x),
               as.ITimeNew(x))
# Unit: milliseconds
#            expr      min       lq     mean   median       uq      max neval
#     as.ITime(x) 705.9513 719.4441 719.6274 723.1317 724.5856 725.0242     5
#  as.ITimeNew(x) 293.5473 293.6914 306.8225 300.4021 313.9687 332.5033     5

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions