是否有用于日期的快速解析器 [英] Is there a fast parser for date

查看:67
本文介绍了是否有用于日期的快速解析器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

For datetimes fasttime provides very fast parsing to 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太长.

Is there something equivalent for dates Date, the lubridate package provides some parser but the fast one (fast_strptime) cast dates to POSIXct (not meant for dates) Casting POSIXct to Date is too long.

鉴于解析到POSIXct的速度有多快,我认为应该有一种与Date一样快的东西

Given how quick it is to parse to POSIXct I would think there should be something as quick to Date

有没有打包好的替代品?

Is there a fast packaged alternative ?

推荐答案

给出

## 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 创建所需的仅日期子集.

If you wanted to go super fast, you could start with tparse.c to create the date-only subset you want.

这篇关于是否有用于日期的快速解析器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆