uribo / zipangu

Japanese utility functions and data
https://uribo.github.io/zipangu
Other
56 stars 7 forks source link

Cache jholiday_spec() within a session #16

Closed yutannihilation closed 2 years ago

yutannihilation commented 4 years ago

https://github.com/r-lib/memoise

uribo commented 4 years ago

なるほど。検討します。(PRも歓迎😁)

uribo commented 4 years ago

いいですね。ぜひ導入しましょう。 jholiday() も非効率的ですものね。

library(zipangu)
library(tictoc)
library(memoise)
library(ggplot2)

jholiday_spec_mem <- 
  memoise(jholiday_spec)

use_cache <- 
  purrr::partial(jholiday_spec_mem, year = 2020, name = "敬老の日", lang = "jp")
currently <- 
  purrr::partial(jholiday_spec, year = 2020, name = "敬老の日", lang = "jp")

tictoc::tic(); invisible(use_cache()); tictoc::toc()
#> 0.184 sec elapsed
tictoc::tic(); invisible(currently()); tictoc::toc()
#> 0.043 sec elapsed

# 二回目
tictoc::tic(); invisible(use_cache()); tictoc::toc()
#> 0.047 sec elapsed
tictoc::tic(); invisible(currently()); tictoc::toc()
#> 0.036 sec elapsed

# 三回目
tictoc::tic(); invisible(use_cache()); tictoc::toc()
#> 0.002 sec elapsed
tictoc::tic(); invisible(currently()); tictoc::toc()
#> 0.041 sec elapsed

x <- 
  bench::mark(
  feature = use_cache(),
  default = currently())
autoplot(x, type = "beeswarm")
#> Loading required namespace: tidyr

Created on 2020-04-05 by the reprex package (v0.3.0)