【问题标题】:Is there a fast parser for date是否有快速的日期解析器
【发布时间】:2016-05-16 18:47:09
【问题描述】:

对于日期时间fasttime 提供对 POSIXct 的非常快速的解析

library('fasttime')
library('lubridate')
library('microbenchmark')

# parse character to POSIXct
Sys.setenv(TZ='UTC')
test <- rep('2011-04-02 11:01:00',1e4)
microbenchmark(
  test1 <- fastPOSIXct(test),
  test2 <- fast_strptime(test,format='%Y-%m-%d %H:%M:%S'),
  test3 <- as.POSIXct(test, format='%Y-%m-%d %H:%M:%S'),
  test4 <- ymd_hms(test),
  times=100)
Unit: microseconds
                                                       expr       min        lq      mean    median         uq       max
                                 test1 <- fastPOSIXct(test)   663.123   692.337  1409.448   701.821   712.4965 71231.585
 test2 <- fast_strptime(test, format = "%Y-%m-%d %H:%M:%S")  1026.342  1257.508  1263.157  1264.928  1273.8145  1366.438
    test3 <- as.POSIXct(test, format = "%Y-%m-%d %H:%M:%S")  9865.265 10060.450 10154.651 10145.551 10186.3030 13358.136
                                     test4 <- ymd_hms(test) 13990.206 17152.779 17278.654 17308.347 17393.6625 22193.544

日期Date 是否有等效的东西,lubridate 包提供了一些解析器,但快速的 (fast_strptime) 将日期转换为 POSIXct(不适用于日期) 将 POSIXct 转换为 Date 太长。

考虑到解析为 POSIXct 的速度有多快,我认为应该有一些快速的日期

有没有快速打包的替代方案?

【问题讨论】:

    标签: r date posixct lubridate


    【解决方案1】:

    给定

    ## the following two (here three) lines are all of fasttime's R/time.R
    fastPOSIXct <- function(x, tz=NULL, required.components = 3L)
      .POSIXct(if (is.character(x)) .Call("parse_ts", x, required.components)
               else .Call("parse_ts", as.character(x), required.components), tz)
    

    因此

    ## so we suggest to just use it, and convert later
    fastDate <- function(x, tz=NULL)
      as.Date(fastPOSIXct(x, tz=tz))
    

    至少胜过as.Date():

    R> library(microbenchmark)
    R> library(fasttime)
    R> d <- rep("2010-11-12", n=1e4)
    R> microbenchmark(fastDate(d), as.Date(d), times=100)
    Unit: microseconds
            expr    min      lq    mean  median      uq     max neval cld
     fastDate(d) 47.469 48.8605 54.3232 55.7270 57.1675 104.447   100  a 
      as.Date(d) 77.194 79.4120 85.3020 85.2585 87.3135 121.979   100   b
    
    R> 
    

    如果您想超快,可以从 tparse.c 开始创建您想要的仅日期子集。

    【讨论】:

    • 你的 RcppBDT 有什么办法可以避免我走 C 路吗?
    • Boost DateTime 中有解析器,但我选择 not 来公开它们,因为您需要链接到库。我们目前不需要纯 时间计算,它们都是基于标题的。并且仅标头使构建和部署变得更加更容易。
    • 只是让您知道您不需要粘贴(x,'12:00:00'),默认情况下它可以正常工作(请参阅文档)
    • 对。当它结束时它会停止解析该步骤。这将使这个解决方案更难被击败......修改帖子和数字。
    • 刚刚意识到它会解析然后在结果上调用 .POSIXct ,如果 as.Date 没有做任何愚蠢的事情,它确实很难被击败。此外,对于 %Y%m%d 等更通用的格式,然后使用来自 lubridate 的 fast_strptime 相同的函数将起作用......
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-10-04
    • 1970-01-01
    • 2016-06-06
    • 1970-01-01
    • 2016-08-24
    • 1970-01-01
    • 2014-10-14
    相关资源
    最近更新 更多