Last active
October 9, 2021 15:36
-
-
Save shuckle16/e7d2fb3f8291ae1c926898ef4595ce36 to your computer and use it in GitHub Desktop.
fast date parsing r
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(data.table) | |
library(tictoc) | |
library(lubridate) | |
dt <- data.table( | |
dayte = format(sample(Sys.Date() + 1:100, size = 1e6, replace = T), format = "%d%b%Y"), | |
a = sample(1000, size = 1e6, replace = T) | |
) | |
tic() | |
a_df <- dt[, .(dt1 = as.Date(dayte, "%d%b%Y"), a)][order(dt1, a)] | |
toc() | |
tic() | |
a_df2 <- merge(dt[, .(dt1 = as.Date(dayte[1], "%d%b%Y")), by = dayte], dt)[,.(dt1, a)][(order(dt1, a))] | |
toc() | |
tic() | |
a_df3 <- dt[, .(dt1 = as.Date(fast_strptime(dayte, format = "%d%b%Y", lt = F)), a)][order(dt1, a)] | |
toc() | |
tic() | |
a_df4 <- merge(dt[, .(dt1 = as.Date(fast_strptime(dayte[1], "%d%b%Y"))), by = dayte], dt)[,.(dt1, a)][(order(dt1, a))] | |
toc() | |
identical(a_df, a_df2) | |
identical(a_df, a_df3) | |
identical(a_df, a_df4) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment