Skip to content

Instantly share code, notes, and snippets.

@Laurae2
Created December 8, 2020 21:38
Show Gist options
  • Save Laurae2/8844e2f5843eda326321e794166d7691 to your computer and use it in GitHub Desktop.
Save Laurae2/8844e2f5843eda326321e794166d7691 to your computer and use it in GitHub Desktop.
R date conversion benchmark copy pasta

Code:

library(anytime)
library(flipTime)
library(data.table)

set.seed(1)
system.time({
  some_dates <- as.Date("2020-12-31") - sample(40000, 1e7, TRUE)
})
system.time({
  some_dates_str <- as.character(some_dates)
})
system.time({
  some_dates_str_dt <- data.table(i = some_dates_str)
})
some_dates_str_dt[, list(Count = .N), by = "i"][order(Count, decreasing = TRUE)]

# Bench
system.time({
  some_dates_rawR <- as.Date(some_dates_str)
})
system.time({
  some_dates_anytime <- anydate(some_dates_str)
})
system.time({
  some_dates_fliptime <- AsDate(some_dates_str, us.format = FALSE)
})
system.time({
  some_dates_rawR_dt <- some_dates_str_dt[, list(o = as.Date(i[1])), by = "i"]
})
system.time({
  some_dates_anytime_dt <- some_dates_str_dt[, list(o = anydate(i[1])), by = "i"]
})
system.time({
  some_dates_fliptime_dt <- some_dates_str_dt[, list(o = AsDate(i[1], us.format = FALSE)), by = "i"]
})

all.equal(some_dates, some_dates_rawR)
all.equal(some_dates, some_dates_anytime)
all.equal(some_dates, some_dates_fliptime)

Run on:

  • Dual Intel Xeon 6154 (36c/72t @3.7 GHz)
  • 768 GB RAM (12x 64 GB 2666 MHz)

Results:

> library(anytime)
> library(flipTime)
> library(data.table)
> 
> set.seed(1)
> system.time({
+   some_dates <- as.Date("2020-12-31") - sample(40000, 1e7, TRUE)
+ })
   user  system elapsed 
  0.688   0.036   0.724 
> system.time({
+   some_dates_str <- as.character(some_dates)
+ })
   user  system elapsed 
 25.417   1.899  27.317 
> system.time({
+   some_dates_str_dt <- data.table(i = some_dates_str)
+ })
   user  system elapsed 
  0.023   0.000   0.023 
> some_dates_str_dt[, list(Count = .N), by = "i"][order(Count, decreasing = TRUE)]
                i Count
    1: 2016-04-30   312
    2: 1973-11-05   312
    3: 1990-06-04   312
    4: 1953-03-11   310
    5: 1941-02-01   309
   ---                 
39996: 2001-01-25   194
39997: 2011-01-31   193
39998: 1951-05-15   192
39999: 1918-01-06   186
40000: 1951-08-15   185
> 
> # Bench
> system.time({
+   some_dates_rawR <- as.Date(some_dates_str)
+ })
   user  system elapsed 
  7.095   5.799  12.892 
> system.time({
+   some_dates_anytime <- anydate(some_dates_str)
+ })
   user  system elapsed 
 13.223   0.002  13.227 
> system.time({
+   some_dates_fliptime <- AsDate(some_dates_str, us.format = FALSE)
+ })
   user  system elapsed 
 12.094   0.077  12.172 
> system.time({
+   some_dates_rawR_dt <- some_dates_str_dt[, list(o = as.Date(i[1])), by = "i"]
+ })
   user  system elapsed 
  3.281   0.122   1.198 
> system.time({
+   some_dates_anytime_dt <- some_dates_str_dt[, list(o = anydate(i[1])), by = "i"]
+ })
   user  system elapsed 
  2.830   0.003   0.481 
> system.time({
+   some_dates_fliptime_dt <- some_dates_str_dt[, list(o = AsDate(i[1], us.format = FALSE)), by = "i"]
+ })
   user  system elapsed 
 39.598   0.303  37.746 
> 
> all.equal(some_dates, some_dates_rawR)
[1] TRUE
> all.equal(some_dates, some_dates_anytime)
[1] "Attributes: < Length mismatch: comparison on first 1 components >"
> all.equal(some_dates, some_dates_fliptime)
[1] TRUE
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment