Get startedGet started for free

Fast parsing with fasttime

The fasttime package provides a single function fastPOSIXct(), designed to read in datetimes formatted according to ISO 8601. Because it only reads in one format, and doesn't have to guess a format, it is really fast!

You'll see how fast in this exercise by comparing how fast it reads in the dates from the Auckland hourly weather data (over 17,000 dates) to lubridates ymd_hms().

To compare run times you'll use the microbenchmark() function from the package of the same name. You pass in as many arguments as you want each being an expression to time.

This exercise is part of the course

Working with Dates and Times in R

View Course

Exercise instructions

We've loaded the datetimes from the Auckland hourly data as strings into the vector dates.

  • Examine the structure of dates to verify it is a string and in the ISO 8601 format.
  • Parse dates with fasttime and pipe to str() to verify fastPOSIXct parses them correctly.
  • Now to compare timing, call microbenchmark where the first argument uses ymd_hms() to parse dates and the second uses fastPOSIXct().

Hands-on interactive exercise

Have a go at this exercise by completing this sample code.

library(microbenchmark)
library(fasttime)

# Examine structure of dates
str(___)

# Use fastPOSIXct() to parse dates
fastPOSIXct(___) %>% str()

# Compare speed of fastPOSIXct() to ymd_hms()
microbenchmark(
  ymd_hms = ___(dates),
  fasttime = ___(dates),
  times = 20)
Edit and Run Code