[This article was first published on NumberTheory » R stuff, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
When trying to parse a large amount of datetime characters into POSXIct objects, it struck me that strftime
and as.POSIXct
where actually quite slow. When using the parsing functions from lubridate
, these where a lot faster. The following benchmark shows this quite nicely.
We have a character vector xi
, which contains about 2.3 million elements. When using strftime
, this takes about 105 seconds on my MacBook pro:
> xi[1:10] [1] "2015-06-03 16:17:03" "2015-06-03 16:17:36" "2015-06-03 16:18:09" [4] "2015-06-03 16:18:48" "2015-06-03 16:19:21" "2015-06-03 16:19:54" [7] "2015-06-03 16:20:27" "2015-06-03 16:21:00" "2015-06-03 16:21:32" [10] "2015-06-03 16:22:05" > system.time(res <- as.POSIXct(xi)) user system elapsed 104.941 1.054 106.201
If we switch to ymd_hms
from lubridate
, we get a very large performance increase:
> library(lubridate) > system.time(res <- ymd_hms(xi)) user system elapsed 0.741 0.136 0.878
ymd_hms
is about a 120 times faster.
To leave a comment for the author, please follow the link and comment on their blog: NumberTheory » R stuff.
R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.