2
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

readr::parse_datetimeは組み込み関数より速いがfast_strptimeより遅い

Last updated at Posted at 2015-08-19

@hoxo_m さんのこんな話がある。
R で文字列を POSIX time に変換するには lubridate::parse_date_time2() がちょっぱや

じゃあreadrパッケージのparse_datetimeだとどうなるだろうと速度比較したのが以下の結果。

Unit: microseconds
                                         expr      min        lq       mean   median        uq      max neval
                      strptime(dates, format)  856.961  875.0410  906.98984  881.281  954.7210 1019.521   100
                            as.POSIXlt(dates) 2641.281 2665.2810 2782.40425 2801.281 2904.3205 2968.642   100
                            as.POSIXct(dates) 3456.321 3499.6805 3628.93529 3608.001 3747.3610 3886.081   100
                 lubridate::fast_strptime(dates, format)   37.121   40.9605   45.22340   45.121   46.7210  120.321   100
 lubridate::parse_date_time(dates, format, locale = "C") 2086.401 2122.0815 2246.79782 2189.281 2295.2010 5060.161   100
              lubridate::parse_date_time2(dates, format)   63.681   69.1210   73.78026   71.361   73.1210  174.402   100
         readr::parse_datetime(dates, format)  219.521  227.0410  248.55141  232.961  245.6005  723.841   100

組み込みよりマシだがlubridateパッケージのfast_strptimeやparse_date_time2ほど速くはない。

コード

コードは以下の通り。


dates <- as.POSIXlt("2014-12-01", tz="Japan") + sample(seq(31*24*60*60)-1, size=100, replace=TRUE)
dates <- as.character(dates)

library(microbenchmark)

format <- "%Y-%m-%d %H:%M:%S"
microbenchmark(
  strptime(dates, format),                    
  as.POSIXlt(dates),                          
  as.POSIXct(dates),                          
  lubridate::fast_strptime(dates, format),               
  lubridate::parse_date_time(dates, format, locale="C"), 
  lubridate::parse_date_time2(dates, format),             
  readr::parse_datetime(dates, format) # new!!!
)

2
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?